You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, Express forwards errors thrown in a route using throw and forwards them to error handling middleware. But it doesnot do something similar if a promise is returned. I have to manually append .catch(next) to the promise to get it forwarded to error handling middleware. For example the following is my code:
// errorHandlers.jsfunctionsequelizeValidationError(err,req,res,next){if(err.name&&err.name=='SequelizeValidationError')res.status(400).send(err.errors)elsenext(err)}// auth.jsrouter.post('/register',middleware.isNotAuthenticated,(req,res,next)=>{const{ email, password, name }=req.body;returnmodels.User.find({where : { email }}).then(user=>{if(user){if(user.password==password)sendToken(user.id,res);elseres.sendStatus(401);}else{returnmodels.User.create({
email, password, name
}).then(user=>{sendToken(user.id,res);})}}).catch(next)})// index.jsrouter.use('/auth',require('./auth'))router.use(errorHandlers.sequelizeValidationError)
It currently fails if I had to forgot to write the catch, instead of automatically calling the error handling middleware. This can result to errors if I somewhere fail to write catch andd IMHO it is inconsistent too.
Also a small question, can I make a middleware to handle this case temporarily? (sorry I know I shouldnt ask this but please)
The text was updated successfully, but these errors were encountered:
Currently, Express forwards errors thrown in a route using
throw
and forwards them to error handling middleware. But it doesnot do something similar if a promise is returned. I have to manually append.catch(next)
to the promise to get it forwarded to error handling middleware. For example the following is my code:It currently fails if I had to forgot to write the catch, instead of automatically calling the error handling middleware. This can result to errors if I somewhere fail to write
catch
andd IMHO it is inconsistent too.Also a small question, can I make a middleware to handle this case temporarily? (sorry I know I shouldnt ask this but please)
The text was updated successfully, but these errors were encountered: