feathersjs-ecosystem/errors

Support array objects as data

Closed this issue · 6 comments

Steps to reproduce

const data = [
    {
        hello: "world"
    }
];
throw new errors.BadRequest("Input not valid", data);

Expected behavior

Response:

{
    "name": "BadRequest",
     "message": "JSON-Schema validation failed",
     "code": 400,
     "className": "bad-request",
     "data": [
        {
            "hello": "world"
        }
     ],
    "errors":  { }
}

Actual behavior

Response:

{
    "name": "BadRequest",
     "message": "JSON-Schema validation failed",
     "code": 400,
     "className": "bad-request",
     "data": {
        "0": {
            "hello": "world"
        }
     },
    "errors":  { }
}

More

The problem is here:
https://github.com/feathersjs/feathers-errors/blob/master/src/index.js#L36

Proposed solution

If we could make the 'errors' property of the data object a parameter instead, we won't have the problem of messing with immutable data and it will also enable us to pass an error object, alongside with an array object as data.

daffl commented

I have a fix for this in #65. Your proposed solution does make sense. The main reason to have an errors property on data was because a lot of the ORM errors already do that. We could add it as another additional parameter but you'd always have to pass an empty data object before the errors.

I think the additional parameter should work, it's better than not being able to pass the errors object at all and it will also keep backwards compatibility.

daffl commented

Can you show an example how you would like to call it?

Example:

const errors = [
    {
      "keyword": "required",
      "dataPath": "",
      "schemaPath": "#/required",
      "params": {
        "missingProperty": "customerId"
      },
      "message": "should have required property 'customerId'"
    }
];
throw new errors.BadRequest("Input validation failed", null, errors);

And in the code, if errors is undefined, we can check the data object for the errors property. No breaking changes.

Should I work on a pull request?
Is this the desired solution?

Sorry late to the game here but any reason data has to be an array or was the issue just that you want errors to be an array? The module should support both already. The only thing you need to do is call it like so:

const errors = [
    {
      "keyword": "required",
      "dataPath": "",
      "schemaPath": "#/required",
      "params": {
        "missingProperty": "customerId"
      },
      "message": "should have required property 'customerId'"
    }
];

throw new errors.BadRequest('Input validation failed', { errors });

Which should result in this:

{
  "name": "BadRequest",
  "message": "JSON-Schema validation failed",
  "code": 400,
  "className": "bad-request",
  "data": {},
  "errors":  [
    {
      "keyword": "required",
      "dataPath": "",
      "schemaPath": "#/required",
      "params": {
        "missingProperty": "customerId"
      },
      "message": "should have required property 'customerId'"
    }
  ]
}