Ember handle custom error payload from server

Hi,

I have a simple ember model called Tag, I have the model as follows: import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),

  notes: DS.hasMany('note')
});

My RESTSerializer for this model is as follows: import DS from 'ember-data';

export default DS.RESTSerializer.extend({
    normalizeResponse(store, primaryModelClass, payload, id, requestType) {
        payload = {
            tags: payload
        };

        return this._super(store, primaryModelClass, payload, id, requestType);
    },

    serializeIntoHash: function(hash, type, record, options) {
        Ember.merge(hash, this.serialize(record, options));
    }
});

when trying to create a new tag, my server gives errors in this format: 400 Bad request { “error”: “test error message” }

However, Ember seems to be expecting errors in some other format. Something like {"errors":[ ] }

How can I override my serializer to expect error payload in the format specified by my server?

Hello Itssasanka,

From my knowledge, handling custom errors is not done from the serializer but from the adapter by overloading the generatedDetailedMessage method. In your case, it could be:

/adapters/my-adapter.js
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  generatedDetailedMessage (status, headers, payload) {
    return payload.error;
  },
}

What @Gilgalas suggested is WRONG. Only use generatedDetailedMessage for debugging purpose.

https://github.com/emberjs/data/blob/master/addon/adapters/rest.js#L1188

If you want to use errors sent from server.

{
  errors: {
    attributeName: [
      'error 1',
      'error 2'
    ]
  }
} 

Then iterate over in

mo.save()
.then(/* code */)
.catch(error) {
  // error.errors.attributeName[0], error.errors.attributeName[1] 
}

By the way, also take a look at similar topic in the forum: Error Handling with Rest Adapter and custom error json - #9 by mtangoo