Handling errors with the (now default) Ember Data JSON-API adapter


#1

Hi.

I am using Ember 1.13.7 and Ember Data 1.13.8, which by default use the JSON-API standard to format the payloads sent to and received from the API.

I would like to use Ember Data’s built-in error handling in order to display red “error” form fields to the user. I have formatted my API error responses as per the JSON-API standard, e.g.

{"errors":[
    {
        "title":"The included.1.attributes.street name field is required.", 
        "code":"API_ERR", 
        "status":"400", 
    }
]}

and when I attempt to save my model the error callback is being correctly executed. If I look within the Ember Inspector I can see that the model’s “isError” value is set to true but I can’t see how Ember Data is supposed to know which field within the model is the one in an error state? I see from the official JSON-API pages (http://jsonapi.org/format/#errors) that you can include a “source” object within the error response:

source: an object containing references to the source of the error, optionally including any of the following members:

pointer: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. “/data” for a primary data object, or "/data/attributes/title" for a specific attribute].

parameter: a string indicating which query parameter caused the error.

but is this what I should be doing in order to tell Ember Data which fields it should mark as being in an error state?

If anyone can help shed some light on this I’d be grateful.

Thanks.


#2

I think you’ve already know the answer to this question as I’ve seen the exact same question on stackoverflow (by a user with the same name). But since I’ve stumbled with the same doubts and I couldn’t find an answer in the documentation and also googling brought me here first i’m going to link it here:

Dealing with errors in Ember DATA (JSON API)

In my case I was returning an HTTP 400 code instead of the expected 422


#3

Partly similar question:

Does anybody know how to handle 404 ‘by handlebars’ in ember components (with JSON-API adapter) ?

dogType: DS.hasMany(‘breed’) // question model parameter

{{#unless question.dogType}}Loading{{/unless}}
{{#each question.dogType as |breed|}}
   {{breed}}
{{/each}}

I’ve try:

  • error templates ( no results )
  • rsvp error handling with instance initializer (throw only The adapter operation was aborted )
  • handleRequest with adapter extend ( no results )
  • error action on route (on controller too ) (no results)
  • use superController and pass there error (no results)
  • tbc…

Error msg:

  1. URL name to JSON-API breed json + 404 (Not found)
  2. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Application breaks here

Version:

Ember 2.13.4
Ember Data 2.16.3

UPDATE I’ve just refactor code to:

  1. first trigger getting breed and showing loader
  2. if not replace loader text with error message and adding try again button

Now it works as I have access to promise on store, however it will be nice to know how to do that in ‘generic’ way.