Search code examples
node.jsrestmongoosehttp-status-codes

REST HTTP Status code if error Mongoose


Which status code i have to use when an error could happen. When get all banks from the mongoose database. The user only does this client action.

Clients action

GET Url: /banks

Mongoose code

Banks
  .find()
  .exec(function(err, banks) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.status(200).jsonp(banks);
    }
  });

I think statuscode 500 instead of 400. Because the client url is valid.


Solution

  • Error codes 4xx denote client errors like, for example, invalid requests. However, in this case the problem is with the DB which is on the server side. So you should use 5xx. In your example, 500 is OK.

    You can find a quick overview of the different codes here: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error