When using promises, I am encountering an issue where the "catch" doesn't seem to catch its child promises. This results in me having to include two instances of the "catch" block.
Facility.userHaveAccess(locationObject.created_by, locationObject.facility_id)
.then(() => {
Locations.create(locationObject)
.then( (result: any) => {
res.send(result)
})
.catch(err => { return res.status(err.status).send({ error: err.message }) })
})
.catch(err => { return res.status(err.status).send({ error: err.message }) })
It's frustrating to have duplicate error handling code since the errors are mostly identical.
However, when attempting to remove the first .catch block, I encounter the following error:
(node:3108) UnhandledPromiseRejectionWarning: #<Object>
(node:3108) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3108) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.