Search code examples
ember.jsjson-api

Handling Ember model error w/ JSONAPI


This has been answer for the MOST part here: Handling errors with the (now default) Ember Data JSON-API adapter

I won't even go into how Ember docs stress to follow JSON API specs (http://jsonapi.org/format/), yet as far as I've read they don't even adhere to them (based off the answer linked to above)... but I digress!

I am not sure how to get errors into my model from the model() hook itself.

For example:

import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        return this.get('store').findAll('page').catch(function(){});
    }
});

I am unable to access the errors in my template as such:

{{#each model.errors as |error|}}
    <div class="error msg_animate">{{{error.message}}}</div>
{{/each}}

I know I am missing a key factor here. Anyone able to point me in the right direction?


Solution

  • Error response should follow the standard JSON API format,

    http://jsonapi.org/format/#errors

    Sample format,

    {
      "errors": [
        {
          "status": "Error status",
          "code": 500,
          "message":"error message",
          "detail": "Some generic non property error message",
          "source": {
            "pointer": "data"
          }
        }
      ]
    }
    

    Most importantly, you need to include error function callback for Promise, Sample,

    model() {
     return this.get('store').findAll('page').then(
         function(result){return result;},
         function(errors){ return errors; });
     }