user275157
user275157

Reputation: 1352

sequelize handling rejection in the create statement - catch not firing

The sequelize create statement has an error and I would like to handle that error. Since the create statement has the error I need to handle promise rejection. How do I do that in code? Tried to look at the sequelize documents but unable to work it out.

db.Employee.create(empData, 
{
    include:[
        {
            model: db.EmployeeDetails
        }
    ]
}).then(function(newEmployee){
    res.json(newEmployee);
}).catch(function(err){
    return next(err);
});

The error is on the create and so the webpage just gives an internal server error. I was under the impression that the catch was something that handled the promise rejection and failure. In this case, how can I handle the promise rejection in code. An example would be greatly appreciated.

Upvotes: 3

Views: 1217

Answers (2)

Ionică Bizău
Ionică Bizău

Reputation: 113405

By doing next(err), by default, you send a 500 Internal Server Error message. If you use Express, and want to show a custom error, just append a status code which is not 5xx to the error:

General Usage:

const err = new Error("my custom error")
err.statusCode = 400
next(err)

In your snippet, do:

db.Employee.create(empData, {
    include:[
        {
            model: db.EmployeeDetails
        }
    ]
}).then(function(newEmployee){
    res.json(newEmployee);
}).catch(function(err){
    err.statusCode = 400
    next(err);
});

If you haven't set your error handler in Express you will need to add somewhere at the end of the main file this:

// Error Handler
app.use(function(err, req, res, next) {
  console.error(err)
  if (!err.statusCode) err.statusCode = 500;
  let msg = err.message
  // Do not expose 500 error messages in production, to the client
  if (process.env.NODE_ENV === "production" && err.statusCode === 500) {
     msg = "Internal Server Error"
  }
  res.status(err.statusCode).send(msg)
})

Upvotes: 1

SpeedyXeon
SpeedyXeon

Reputation: 136

Your webpage showing a 500 error means the issue was caught / working as intended. What you need to do is figure out how to handle displaying that error in a pretty format - this being a UI task. If you want a 'patch' for hiding the issue, change your return to a res. This will trick your browser with a 200 status and hide the error.

I do want to add, I recommend trying async/await for sequelize. There's a good amount of usage examples with it.

Promise

db.Employee.create(empData, 
{
    include:[
        {
            model: db.EmployeeDetails
        }
    ]
}).then(function(newEmployee){
    res.json(newEmployee);
}).catch(function(err){
    // Temporary patch
    res.json("pretty error message");
});

Async/Await version

async function createEmployee(empData) {
  try {
    return await db.Employee.create(empData, {
      include:[ { model: db.EmployeeDetails } ]
    });
  } catch (err) {
    // Handle error here
    return err;
  }
}

Upvotes: 0

Related Questions