I have a simple web application using koa and would like to crash the process when an error occurs that I was not expecting. Unfortunately koa seems to always catch the error, report it to stderr and keeps the process running. Is there a way to override this behaviour?
I have reviewed Does Koa safely handle errors? and it mentions 2 scenarios that koa will not trap, but it states that the first one is malformed and I cannot quite figure out how to use the second one.
For reference I have the following error handler which tries to report the failure to the user with a unique'ish id and then crash using the setImmediate method:
app.use(function* (next) {
try {
yield next;
} catch (err) {
// https://stackoverflow.com/questions/10985872/nodejs-generate-short-unique-alphanumeric
var tuple = this.request.socket.remoteAddress + ":" + this.request.socket.remotePort;
var errorSession = parseInt(tuple.replace(/[^\d]/g, ''), 10).toString(36).toUpperCase();
err.message = "["+errorSession+"] "+err.message;
logger.error(err);
this.status = err.status || 500;
yield this.render('unknown-error', { errorSession : errorSession });
// this kinda works, but is considered malformed
setImmediate(function () { throw err;});
}
});