Skip to content

Instantly share code, notes, and snippets.

@rgould
Created March 13, 2014 05:31
Show Gist options
  • Save rgould/9522350 to your computer and use it in GitHub Desktop.
Save rgould/9522350 to your computer and use it in GitHub Desktop.
Using Sentry-Raven with Meteor on both client and server, catching uncaught exceptions. Uses this PR: https://github.com/deepwell/meteor-raven/pull/4
function initializeRaven() {
/*
* SENTRY_DSN is provided as an environment variable on Heroku, and is
* inaccessible on the client. We added this method to make it accessible.
*/
Meteor.call('getSentryDSN', function(error, sentryDSN) {
if (error) throw error;
RavenLogger.initialize({
client: sentryDSN
});
/*
* The rest of this is used to trap exceptions from within Deps.autorun(), Meteor.startup(), etc.
*/
var orig = Meteor._debug;
Meteor._debug = function() {
/*
* `arguments` are usually strings. 0 is the debug message from Meteor,
* 1 is the stack trace or error message. If it's a stack trace as a
* string, it will cause raven to receive an error (query string too
* long), so we will rebuild the Error object so that Raven.js submits
* it to Sentry properly.
*/
var buffer = _.reduce(arguments, function(memo, arg) {
return memo + " - " + arg;
}, "");
var err = new Error(buffer);
RavenLogger.log.call(RavenLogger, err);
orig.apply(Meteor, arguments);
};
});
}
/*
* This code is here so that it is loaded earliest. Otherwise we may miss
* some exceptions that occur on load, before the async call comes back.
*/
if (Meteor.isClient) {
Meteor.startup(function() {
initializeRaven();
});
}
var ENV_VAR = 'SENTRY_DSN';
function stripPrivateKey(sentryDSN) {
if (sentryDSN) {
// https://foo:bar@app.getsentry.com/baz - we want everything but ':bar'
return sentryDSN.replace(/:\w+/, '');
}
}
function initializeRaven() {
var sentryDSN = process.env[ENV_VAR];
if (sentryDSN) {
RavenLogger.initialize({
server: sentryDSN
}, {
patchGlobal: function(isLogged, message) {
console.log("Uncaught exception: ", message);
process.exit(1);
}
});
}
}
Meteor.methods({
getSentryDSN: function() {
return stripPrivateKey(process.env[ENV_VAR]);
}
});
initializeRaven();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment