Better handling of AJAX errors

This commit is contained in:
Toby Zerner 2015-02-03 18:40:34 +10:30
parent 815fa2b025
commit aa665cfd79
2 changed files with 25 additions and 7 deletions

View File

@ -1,9 +1,8 @@
import DS from 'ember-data';
import JsonApiAdapter from 'ember-json-api/json-api-adapter';
import config from '../config/environment';
export default JsonApiAdapter.extend({
// Todo: make this loaded via an environment variable or something
host: config.apiURL,
// We can get rid of this after
@ -13,6 +12,22 @@ export default JsonApiAdapter.extend({
store.metaForType(type, payload.meta);
delete payload.meta;
}
},
ajaxError: function(jqXHR) {
var errors = this._super(jqXHR);
if (errors instanceof DS.InvalidError) {
var newErrors = {};
for (var i in errors.errors) {
var error = errors.errors[i];
newErrors[error.path] = error.detail;
}
errors = new DS.InvalidError(newErrors);
} else if (errors instanceof JsonApiAdapter.ServerError) {
// @todo show an alert message
console.log(errors);
}
return errors;
}
});

View File

@ -88,11 +88,14 @@ export default Ember.ObjectController.extend(Ember.Evented, {
}
},
function(reason) {
var errors = reason.errors;
for (var i in reason.errors) {
var message = AlertMessage.create({
type: 'warning',
message: reason.errors[0].detail
message: reason.errors[i]
});
controller.get('controllers.application').send('alert', message);
}
})
.finally(function() {
composer.set('content.loading', false);