Catching and logging uncaught errors from Meteor methods

If a Meteor method throws an exception and it is not caught by the client, it is written to the log file. Has anyone had any success capturing these exceptions and logging them somewhere else outside of the app log file? I’d like to be able to send these errors to a cloud error reporting service, but it seems like the only way to catch these errors is from the client code that called the method in the first place, which is not ideal since some methods are called inside packages. I’d like to build a generic handler that captures all uncaught errors, to have a unified way of keeping track of such issues.

https://www.loggly.com/blog/managing-a-meteor-application-in-production-three-real-log-management-use-cases/

case #2 might be interesting for you?

I’m using that technique to catch client errors, but I don’t think onerror will be triggered by an uncaught error on the server that happens in a method.

Have you tried to wrap your calls with a function that sends the error to your cloud service? Something like this available to your client and server?

Meteor.methods({
	"wrapper": function (methodName, arguments) {
		check(methodName, String);
		check(arguments, [Match.Any]);

		Meteor.call("methodName", arguments, function (error, result) {
			if (!error) {
				// Handle Success
			} else {
				// send error to cloud service
			}
		});
	}
});

I like this approach but it wouldn’t catch method calls made by packages where I don’t have control over the calling code (without modifying the package). I’d also love to use a technique that will also catch other server exceptions (e.g. exception thrown by a publication).

I don’t know how we could wrap package exceptions right now, but for publications you could do something like what i wrote above available only for the client.

subscribe = function (pubName, arguments) {
	check(pubName, String);
	check(arguments, [Match.Any]);

	var callbacks = {
		onReady: function() {
			// Handle Success
		},
		onStop: function(error) {
			// send error to cloud service
		}
	};

	if (arguments.length) {
		Meteor.subscribe(pubName, arguments, callbacks);
	} else {
		Meteor.subscribe(pubName, callbacks);
	}
}
1 Like