ndelvalle/generator-api

How to use 'lib/controller' handleError and validationError?

syz3r opened this issue · 1 comments

syz3r commented

Hi,

I can see handleError and validationError functions inside lib/controller but they are not used any where. Could you please explain how to use them generated controllers.? Do you have any suggestion about how to do effiecent global error handling.

  • Sushil

Hi @syz3r the generator does not come with any handleError and validationError method inside the controller. For error handling I'll suggest to manage it inside each controller method and return the correct http status code. Also it could be useful to create an express error middleware like:

const errorMiddleware = (err, req, res, next) => {
  const statusCode = err.statusCode || 500;
  // do some error handling
  res.status(statusCode).send(err);
};