2015-02-03 18:40:34 +10:30
|
|
|
import DS from 'ember-data';
|
2014-12-23 12:45:07 +10:30
|
|
|
import JsonApiAdapter from 'ember-json-api/json-api-adapter';
|
2015-02-10 18:05:40 +10:30
|
|
|
|
|
|
|
import config from 'flarum/config/environment';
|
|
|
|
import AlertMessage from 'flarum/components/ui/alert-message';
|
2014-12-20 16:56:46 +10:30
|
|
|
|
2015-01-16 17:15:23 +10:30
|
|
|
export default JsonApiAdapter.extend({
|
2015-01-30 12:05:57 +10:30
|
|
|
host: config.apiURL,
|
|
|
|
|
2015-02-03 18:40:34 +10:30
|
|
|
ajaxError: function(jqXHR) {
|
|
|
|
var errors = this._super(jqXHR);
|
2015-02-10 18:05:40 +10:30
|
|
|
|
|
|
|
// Reparse the errors in accordance with the JSON-API spec to fit with
|
|
|
|
// Ember Data style. Hopefully something like this will eventually be a
|
|
|
|
// part of the JsonApiAdapter.
|
2015-02-03 18:40:34 +10:30
|
|
|
if (errors instanceof DS.InvalidError) {
|
|
|
|
var newErrors = {};
|
|
|
|
for (var i in errors.errors) {
|
|
|
|
var error = errors.errors[i];
|
|
|
|
newErrors[error.path] = error.detail;
|
|
|
|
}
|
2015-02-10 18:05:40 +10:30
|
|
|
return new DS.InvalidError(newErrors);
|
2015-02-03 18:40:34 +10:30
|
|
|
}
|
2015-02-10 18:05:40 +10:30
|
|
|
|
|
|
|
// If it's a server error, show an alert message. The alerts controller
|
|
|
|
// has been injected into this adapter.
|
|
|
|
if (errors instanceof JsonApiAdapter.ServerError) {
|
|
|
|
var message = AlertMessage.create({
|
|
|
|
type: 'warning',
|
2015-02-12 14:34:07 +10:30
|
|
|
message: 'Something went wrong: '+errors.message.errors[0].code
|
2015-02-10 18:05:40 +10:30
|
|
|
});
|
|
|
|
this.get('alerts').send('alert', message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:40:34 +10:30
|
|
|
return errors;
|
2015-01-30 12:05:57 +10:30
|
|
|
}
|
2015-02-09 20:14:18 +10:30
|
|
|
});
|