feathersjs-ecosystem/errors

Passing errors as second argument

Closed this issue · 3 comments

Hi,
I would like to pass errors object as second argument in the class constructor, something like:

return next new app.errors.BadRequest "Invalid fields", name: "Empty value", email: "Invalid email address"

Response:

{
  "code": 400,
  "name": "BadRequest",
  "message": "Invalid fields",
  "errors": {
    "name": "Empty value",
    "email": "Invalid email address"
  }
}

Right now to reproduce this output I have to:

err = new app.errors.BadRequest "Invalid fields"
err.errors = 
  name: "Empty value"
  email: "Invalid email address"
return next err

That seems pretty valid. I'll look to get that in shortly, unless you have a PR handy 😉

We're going to include this in the boiler plate generator and drop the handlers from this repo as described here: #11.

Whenever you create an error you can now pass errors or data as a primary or secondary argument.

For example:

var err1 = new errors.BadRequest('Invalid Fields', {errors: {name: 'Empty Value', email: 'Invalid Email'} });

// or

var err2 = new errors.BadRequest({errors: {name: 'Empty Value', email: 'Invalid Email'} });