Catching an error in an async function in Node/Express
This has been documented in express
error handling doc:
You must catch errors that occur in asynchronous code invoked by route handlers or middleware and pass them to Express for processing. For example:
app.get('/', function (req, res, next) { setTimeout(function () { try { throw new Error('BROKEN') } catch (err) { next(err) } }, 100) })
The above example uses a try...catch block to catch errors in the asynchronous code and pass them to Express. If the try...catch block were omitted, Express would not catch the error since it is not part of the synchronous handler code.
So, basically you need to try..catch
the route. (the examples are basically same, mother of coincidence)