Description
Goal: swimlane code by catching errors, and:
- report error to Sentry with custom tags
- log error in the best possible way
… whilst reporting uncaught exceptions to Sentry as well.
I want to swimlane my JS, so if one function fails, the next will run regardless. When there is an error, I want to capture it with Raven, and log it to the console.
var swimlane = function (options, fn) {
try { fn(); } catch(e) {
raven.captureException(e, options);
// optionally console log the error
// console.error(error);
}
};
swimlane({ tags: { module: 'foo' } }, foo());
swimlane({ tags: { module: 'bar' } }, bar());
To do this, I have written a function similar to raven.wrap
, exception it does not re-throw, thus allowing execution to continue.
However, I'm finding that logging the error the console is not easy. Each browser has its own way of logging the error to the console. For example, you might try:
console.error(error);
In Chrome, console.error
creates a new error object and the argument is the message. So you end up with an error nested in an error:
http://jsbin.com/vimuxu/1/edit?js,output
In browsers that support the error.stack
property (non-standard), I would like to log that. Otherwise, I would like to log the file name, line number, and column number. This logging is essentially what the browsers do automatically for uncaught exceptions.
Because of this, I would much rather let the browser automatically log my errors to the console as uncaught exceptions.
To do that, I could write a swimlane
function that just catches the error and throws it in the next turn so it doesn't halt the current execution stack:
raven.install();
var swimlane = function (options, fn) {
try { fn(); } catch(e) {
setTimeout(function () { throw e; });
}
};
swimlane({ tags: { module: 'foo' } }, foo());
swimlane({ tags: { module: 'bar' } }, bar());
The trouble then is I have no way of specifying my options (in this case, tags). I could use raven.setExtraContent/setTagsContext
, but that just permanently updates the global options with the new tags.
The best of both worlds would be to capture the error and throw it in the next turn (for browser logging), but then it would be logged twice because raven is also listening for uncaught exceptions:
raven.install();
var swimlane = function (options, fn) {
try { raven.context(options, fn); } catch(e) {
setTimeout(function () { throw e; });
}
};
swimlane({ tags: { module: 'foo' } }, foo());
swimlane({ tags: { module: 'bar' } }, bar());
Is there a happy medium? Perhaps you might be able to shed some light on where I'm going wrong. Or, perhaps you know of a good way to manually log errors across all browsers (stack in modern, file + line and col numbers in non-modern (i.e. IE 9)).