mardi 24 juillet 2018

Laravel API pass multiple errors using ValidationException, but with customised status code/title

I am currently using MessageBag and ValidationException class to return back an array of errors to my API users. The problem with this is that it always returns a 422, validation exception

{
  "errors": [
    {
      "status": 422,
      "title": "Validation Exception",
      "field": "consignment.networkCode",
      "detail": "Service Denied"
    },
    {
      "status": 422,
      "title": "Validation Exception",
      "field": "consignment.networkCode2",
      "detail": "Service Denied"
    }
  ]
}

The use of message bag and this ValidationException provides an easy way to send back multiple API errors. But they are not necessarily validation errors. I looked at extending the class but could not figure out a way that I can customise the status and title for each message bag entry. Is this possible or is there a better way to respond API errors of this kind?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire