I want to be able to have a function that is called unhandled Promise rejections that happen inside a Restify middleware, to send the error to user. There is server.on('uncaughtException', ...) that catches all the errors, but if my middleware is async function or it returns a Promise, this handler won't catch it.
Example:
// server creation is omitted
server.on('uncaughtException', (req, res, route, err) => {
console.log(err);
res.send({ success: false, error: err.message });
});
server.get('/', async (res, req, next) => {
throw new Error('Some error');
})
If I run this example, it won't call the error handler and will print this instead:
(node:23037) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Some error
Restify 4.3.0, tested with Node.js 6.10.0 (compiled with Babel) and 7.9.0 (it supports async natively)