Skip to content

Instantly share code, notes, and snippets.

@yetzt
Last active September 1, 2016 16:16
Show Gist options
  • Save yetzt/e94e125088218f1000f643732d10a8f2 to your computer and use it in GitHub Desktop.
Save yetzt/e94e125088218f1000f643732d10a8f2 to your computer and use it in GitHub Desktop.
excepto patronum: post uncaught exceptions to slack
#!/usr/bin/env node
/*
excepto patronum!
a small, vanilla node script to post uncaught exceptions to slack
*/
var https = require("https");
var querystring = require("querystring");
function ent(str){ return (str||"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/&amp;([a-z0-9]{2,7});/g,"&$1;"); };
process.on("uncaughtException",function(e){
try {
var payload = querystring.stringify({
payload: JSON.stringify({
channel: "#channel",
username: "exceptionbot",
icon_emoji: ":boom:",
attachments: [{
pretext: "Error in `"+require.main.filename+"`:",
text: ent(e.message)+"\n\n```"+ent(e.stack)+"```\n",
ts: Math.round(Date.now()/1000),
mrkdwn_in: ["text","pretext"],
color: "#FF0000"
}]
})
});
} catch (e){
process.exit();
}
var r = https.request({
hostname: 'hooks.slack.com',
port: 443,
path: '/services/OMG/WTF/BBQ',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(payload)
}
}, function(res){
process.exit();
});
r.on('error', process.exit);
r.write(payload);
r.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment