Skip to content

Custom error response code and message #59

@mikkelfish

Description

@mikkelfish

Hello, right now I see that response codes for errors are hardcoded to the route. This can cause confusion when there is an internal error because the client thinks it is a bad request.

In addition, the error message does not serialize correctly when passing an Error object, which several other middlewares require.

I have locally hacked

exports.respondOrErr = function(res, errStatusCode, err, statusCode, content) {
  if (err) {
    exports.respond(res, err.statusCode || errStatusCode, err.message || err);
  } else {
    exports.respond(res, statusCode, content);
  }
};

and then am passing an Error like so

var error = new Error(err);
error.statusCode = 500;
next(error); 

Is this satisfactory/best practice? Didn't feel like making a pull request for such a minor change without knowing that the pattern was acceptable.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions