ra-jsonapi-client icon indicating copy to clipboard operation
ra-jsonapi-client copied to clipboard

HttpError is called with response body

Open euoia opened this issue 3 years ago • 0 comments

Hi @henvo - thanks for creating this library. For background, I am trying to hook react-admin up to a Laravel back end using https://laraveljsonapi.io/. It's going okay so far.

I noticed that when an HTTP error occurs (for example because of sending an attribute that isn't allowed), react-admin shows a blank screen and I get a console error Polyglot.transformPhrase expects argument #1 to be string.

I had a look at the error handling and the following code treats the entire response body a message field for the error: https://github.com/henvo/ra-jsonapi-client/blob/master/src/initializer.js#L39

The JSON:API spec says that errors should include a top-level errors array: https://jsonapi.org/format/1.1/#errors

And indeed this is what laravel-json-api returns:

{
   "jsonapi":{
      "version":"1.0"
   },
   "errors":[
      {
         "detail":"The field companyId is not a supported attribute.",
         "source":{
            "pointer":"\/data\/attributes"
         },
         "status":"400",
         "title":"Non-Compliant JSON:API Document"
      }
   ]
}

I am unfamiliar with what level of detail react-admin supports for errors, but perhaps we should attempt to extract error information from the response, and fall back to using the entire response body only if it's a string.

Happy to submit a PR if it might be accepted.

euoia avatar Dec 11 '22 10:12 euoia