Where to handle the error in a sequelize ORM query statement? Where to handle the error in a sequelize ORM query statement? express express

Where to handle the error in a sequelize ORM query statement?


If you want to handle the specific error, attach a .catch handler

models.Item.create({  title : req.body.title,  UserId : req.body.UserId}).then(function(item){  res.json({    "Message" : "Created item.",    "Item" : item  });}).catch(function (err) {  // handle error;});

If you want to handle errors more generally (i.e. show a nice error message, instead of killing your server, you might want to have a look at unhandledexception

https://nodejs.org/api/process.html#process_event_uncaughtexception

If you are using express, it also contains some error handling facilities http://expressjs.com/en/guide/error-handling.html


You can handle all ORM errors to return an HTTP 500 status and some default error message by doing something like below

router.use((error, request, response, next) => {  response.status(error.status || 500).json({    status: 'error',    error: {      message: error.message || serverErrorMsg,    },  });});

The above code also allows you to throw errors with custom messages that will be sent to the client