Skip to content

Instantly share code, notes, and snippets.

@kylewelsby
Last active December 31, 2015 20:09
Show Gist options
  • Save kylewelsby/8038762 to your computer and use it in GitHub Desktop.
Save kylewelsby/8038762 to your computer and use it in GitHub Desktop.
A useful bit of code to be able to report PhantomJS errors to BugSnag
/* global require, phantom */
(function () {
'use strict';
var exec, system, env;
exec = require('child_process').spawn;
system = require('system');
env = system.env.NODE_ENV || system.env.RACK_ENV || 'development';
phantom.onError = function (message, trace) {
// dont report for development
if (env === 'development') {
return;
}
var payload, stacktrace, stack;
stacktrace = [];
if (trace && trace.length) {
trace.forEach(function (t) {
stack = {
file: t.file || t.sourceURL,
lineNumber: t.line,
columnNumber: t.column,
method: t['function'],
inProject: true
};
stacktrace.push(stack);
});
}
payload = {
apiKey: 'c9d60ae4c7e70c4b6c4ebd3e8056d2b8',
notifier: {
name: "Bugsnag PhantomJS",
version: "0.0.0",
url: "https://gist.github.com/kylewelsby/8038762"
},
events: [{
releaseStage: env,
context: "src/node/crawler.js",
groupingHash: message,
exceptions: [{
errorClass: 'phantom.onError',
message: message,
stacktrace: stacktrace
}]
}]
};
try {
exec('curl', [
"-H",
"Content-Type: application/json",
"--data",
JSON.stringify(payload),
"https://notify.bugsnag.com"
]);
} catch (err) {
console.error('Could not report this to bugsnag');
console.error(err);
}
};
}).call();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment