Skip to content

Commit c696723

Browse files
authored
Merge pull request #15 from qonto/extract-errors
Handle backend errors deserialization
2 parents ba369ad + 8b27d4a commit c696723

File tree

7 files changed

+10711
-13
lines changed

7 files changed

+10711
-13
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ let payload = { publisher: user };
4343

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

addon/actions/action.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
/* eslint ember-suave/no-direct-property-access:1 */
2-
31
import Ember from 'ember';
4-
52
import UrlBuilder from '../utils/url-builder';
63
import normalizePayload from '../utils/normalize-payload';
74
import defaultConfig from '../config';
@@ -14,7 +11,9 @@ const {
1411
ObjectProxy,
1512
ArrayProxy,
1613
PromiseProxyMixin,
17-
typeOf: emberTypeOf
14+
typeOf: emberTypeOf,
15+
isArray,
16+
RSVP
1817
} = Ember;
1918

2019
const promiseTypes = {
@@ -88,13 +87,27 @@ export default emberObject.extend({
8887
},
8988

9089
_promise() {
91-
return this.get('adapter').ajax(this.get('url'), this.get('requestType'), this.get('data')).then((response) => {
92-
if (this.get('config.pushToStore') && this._validResponse(response)) {
93-
return this.get('serializer').pushPayload(this.get('store'), response);
94-
} else {
95-
return response;
96-
}
97-
});
90+
return this.get('adapter')
91+
.ajax(this.get('url'), this.get('requestType'), this.get('data'))
92+
.then(this._onSuccess.bind(this), this._onError.bind(this));
93+
},
94+
95+
_onSuccess(response) {
96+
if (this.get('config.pushToStore') && this._validResponse(response)) {
97+
return this.get('serializer').pushPayload(this.get('store'), response);
98+
}
99+
100+
return response;
101+
},
102+
103+
_onError(error) {
104+
if (this.get('config.pushToStore') && isArray(error.errors)) {
105+
let id = this.get('model.id');
106+
let typeClass = this.get('model').constructor;
107+
error.serializedErrors = this.get('serializer').extractErrors(this.get('store'), typeClass, error, id);
108+
}
109+
110+
return RSVP.reject(error);
98111
},
99112

100113
_validResponse(object) {

0 commit comments

Comments
 (0)