So my code is like this:
router.get('/myapi/someotherapi/:id', (request, response) => {
console.log('api: GET /admin/myapi/someotherapi/:id');
console.log('Reject star redeem requests by id');
auth.verifyToken(request, response, { role: 'Admin' }).then(b_tokenValidated => {
if (b_tokenValidated && b_tokenValidated.statusCode !== 401) {
const myId = b_tokenValidated.userId;
const updateLastActive = user.updateLastActive(myId, request);
const mySpecialFunction = myLib.mySpecialFunction(
request.params.id, myId
);
updateLastActive.then(() => {
mySpecialFunction.then(r => {
generalApiResponseSender(response, r);
})
.catch((err) => {
console.log('mySpecialFunction failed: ', err);
generalApiErrorHandler(response, err);
})
})
.catch((err) => {
generalApiErrorHandler(response, err);
})
}
else {
generalApiErrorHandler(
response,
{
status: 401,
message: "You haven't logged in or token has expired."
}
)
}
}).catch((err) => {
console.log('verifyToken failed: ', err);
let errCode = 401;
let errMsg = "You haven't logged in or token has expired.";
if (typeof err === 'number') {
errCode = err;
errMsg = "Please see error code for more details.";
}
generalApiErrorHandler(
response,
{
status: errCode,
message: errMsg
}
)
})
})
export const generalApiErrorHandler = (response, err) => {
if (!response.headersSent) {
if (err['status'] && err['message']) {
response.status(err['status']).send(err['message']);
}
else if (typeof err === 'number') {
response.sendStatus(err);
}
else {
response.sendStatus(500);
}
}
}
export const generalApiResponseSender = (response, data) => {
if (!response.headersSent) {
if (typeof data !== "number") {
response.send(snake2Camel(data));
}
else {
response.sendStatus(data);
}
}
}
And I am getting errors like this:
api: GET /myapi/someotherapi/:id
My function is triggered
id: 30
myId: 1
Request not found, abort.
update_query failed, error: 404
mySpecialFunction failed: 404
api: GET /myapi/someotherapi/:id
My function is triggered
id: 30
myId: 1
Request not found, abort.
update_query failed, error: 404
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
[UnhandledPromiseRejection: 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(). The promise rejected with the reason "404".] {
code: 'ERR_UNHANDLED_REJECTION'
}
As you can see, these two call are exactly the same.
The first call triggers the error handler normally and send the error code back to the frontend.
The second call, however, declares that there was no error handler to take care of it, so it chooses to stop the whole application.
Can anyone explain to me why is this happening and how can I prevent it? What can be the reason for the error not being caught by the catch function in the second time?
I guess using return instead of throw could potentially solve the issue, since I define that 404 error. But I don’t believe the best practice would be returning the error code just like it is not an error but a valid return value.
PS. I spotted someone said async
won’t work with catch (UnhandledPromiseRejection despite catching expection), so I removed all async
and await
from the function. Yet, this still happens.