Our model layer in our app will generate an array of errors that may or may not contain objects. As an example, say someone wanted to POST a thing to our api and the user submitted an invalid payload, an example validation error array may look like so:
["foo is required",
"bar must be a string",
{ orders: ["id is required", "name must be a string"]}]
Notice how orders is an object - that's because orders is a an object with its own properties that should be posted in the payload and we wanted to namespace any validation errors under that object to make it more clear to the end user.
All is fine and dandy until our framework calls new Error(validationErrors) before returning a 400 Bad Request.
This is what the error message ends up looking like:
{"statusCode": 400,
"error":"Bad Request",
"message":"foo is required,bar must be a string, [object Object]"}
You can see that the nested orders validation object has been lost.
As a short-term fix, I've JSON.stringified the validationErrors array, but that ends up causing the error to look like:
{"statusCode":400,
"error":"Bad Request",
"message":"[\"the value of active is not allowed to be undefined\",\"the value of name is not allowed to be undefined\",\"the value of team is not allowed to be undefined\",\"the value of startDate is not allowed to be undefined\",\"the value of endDate is not allowed to be undefined\",{\"location\":[\"the value of name is not allowed to be undefined\",\"the value of latitude is not allowed to be undefined\",\"the value of longitude is not allowed to be undefined\"]}]"}
Is there a better solution to this problem?