Reputation: 2164
How to change promise reject()
error from 500 to 200 with status code ok?
Always when i trying to reject in some cases i'm getting 500 error on frontend.
export default function login(req, res) {
return new Promise((resolve, reject) => {
passport.authenticate( 'local', function authenticate(err, user, info){
if(err) reject(err);
if(!user) reject(info);
req.logIn(user, err, info => {
if(err) { reject(err) }
else {
resolve(user);
}
})
})(req, res);
});
}
error: Status Code:500 Internal Server Error
Upvotes: 1
Views: 66
Reputation: 128786
500 is a HTTP error, not something which is generated by your promise, but instead something which is returned from your server.
If you're wanting to avoid the 500 error, the best solution would be to work out why you're getting that error on the back-end and fix it there, as a 500 usually means an uncaught exception was thrown.
However if you're wanting to pretend everything is okay, you can ignore the 500 error within your code and return something which isn't the generated error:
if (err)
reject(true);
Then within your Promise's catch
block, you can:
login(...)
.catch(function(e) {
if (e === true)
// pretend everything is okay
else
...
})
Upvotes: 2
Reputation: 1544
If you are rejecting then the status code should not be 200 OK but still if you want to do it, you need to do it just before you send the response back,
Here,
res.status(200).send('Something broke!');
Upvotes: 0