Trying to catch UnhandledPromiseRejectionWarning: Error: 404 Not Found
Date : March 29 2020, 07:55 AM
like below fixes the issue The problem is that you're tripping over the new Promise antipattern, and not propagating errors correctly while doing so. :-) The correct way to fix it is not to use new Promise at all: const promise = snekfetch.get('https://www.website.com/api/public/users?name=' + user)
.then(body => {
const json = JSON.parse(body.text);
const name = json.name;
return name;
});
// NOT APPROPRIATE HERE, but how you'd do it if you used an explicit new promise
const promise = new Promise((resolve, reject) => {
snekfetch.get('https://www.website.com/api/public/users?name=' + user)
.then(body => {
const json = JSON.parse(body.text);
const name = json.name;
resolve(name);
})
.catch(reject);
});
|
UnhandledPromiseRejectionWarning on async await promise
Date : March 29 2020, 07:55 AM
may help you . UnhandledPromiseRejectionWarning on async await promise , Wrap your code in try-catch block. async function foobar() {
try {
await foo() ? console.log("Have foo") : console.log("Not have foo");
}
catch(e) {
console.log('Catch an error: ', e)
}
}
|
Axios request : promise error UnhandledPromiseRejectionWarning after catch anyway
Date : March 29 2020, 07:55 AM
I wish this help you The catch block is typically used to recover from error. Any error thrown and not caught in a promise chain (inside the then, catch, etc.) will result in UnhandledPromiseRejection. If you are sure that this request failing does not introduce any undefined state into your app, you could just log the error and not throw. .catch(err => logger.error(err)) // or console.error()
process.on('unhandledRejection', reason => {
throw reason;
});
|
UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch b
Date : March 29 2020, 07:55 AM
will help you .catch(error => { throw error}) is a no-op. It results in unhandled rejection in route handler. As explained in this answer, Express doesn't support promises, all rejections should be handled manually: router.get("/emailfetch", authCheck, async (req, res, next) => {
try {
//listing messages in users mailbox
let emailFetch = await gmaiLHelper.getEmails(req.user._doc.profile_id , '/messages', req.user.accessToken)
emailFetch = emailFetch.data
res.send(emailFetch)
} catch (err) {
next(err);
}
})
|
async/await throws error "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'xyz' of undefined"
Date : March 29 2020, 07:55 AM
it helps some times Although you should always use a try...catch block around async/await code, this will not help solve the problem of "why does it sometimes work and sometimes fail?" Your problem is in this block of code: } else {
console.log(`Invalid input '${procedure}': Please select again\n`);
await this.requestProcedure();
}
} else {
console.log(`Invalid input '${procedure}': Please select again\n`);
return await this.requestProcedure();
}
|