Skip to content

Commit

Permalink
Merge pull request #15 from qonto/extract-errors
Browse files Browse the repository at this point in the history
Handle backend errors deserialization
  • Loading branch information
Exelord authored Jun 20, 2017
2 parents ba369ad + 8b27d4a commit c696723
Show file tree
Hide file tree
Showing 7 changed files with 10,711 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ let payload = { publisher: user };

postToPublish.publish(payload).then((status) => {
alert(`Post has been: ${status}`)
}).catch((error) => {
console.log('Here are you serialized model errors', error.serializedErrors);
});
```

Expand Down
35 changes: 24 additions & 11 deletions addon/actions/action.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint ember-suave/no-direct-property-access:1 */

import Ember from 'ember';

import UrlBuilder from '../utils/url-builder';
import normalizePayload from '../utils/normalize-payload';
import defaultConfig from '../config';
Expand All @@ -14,7 +11,9 @@ const {
ObjectProxy,
ArrayProxy,
PromiseProxyMixin,
typeOf: emberTypeOf
typeOf: emberTypeOf,
isArray,
RSVP
} = Ember;

const promiseTypes = {
Expand Down Expand Up @@ -88,13 +87,27 @@ export default emberObject.extend({
},

_promise() {
return this.get('adapter').ajax(this.get('url'), this.get('requestType'), this.get('data')).then((response) => {
if (this.get('config.pushToStore') && this._validResponse(response)) {
return this.get('serializer').pushPayload(this.get('store'), response);
} else {
return response;
}
});
return this.get('adapter')
.ajax(this.get('url'), this.get('requestType'), this.get('data'))
.then(this._onSuccess.bind(this), this._onError.bind(this));
},

_onSuccess(response) {
if (this.get('config.pushToStore') && this._validResponse(response)) {
return this.get('serializer').pushPayload(this.get('store'), response);
}

return response;
},

_onError(error) {
if (this.get('config.pushToStore') && isArray(error.errors)) {
let id = this.get('model.id');
let typeClass = this.get('model').constructor;
error.serializedErrors = this.get('serializer').extractErrors(this.get('store'), typeClass, error, id);
}

return RSVP.reject(error);
},

_validResponse(object) {
Expand Down
Loading

0 comments on commit c696723

Please sign in to comment.