excepto patronum: post uncaught exceptions to slack
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
#!/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,"&").replace(/</g,"<").replace(/>/g,">").replace(/&([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