Last active
December 31, 2015 20:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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