2015-02-03 16:10:34 +08:00
|
|
|
import DS from 'ember-data';
|
2014-12-23 10:15:07 +08:00
|
|
|
import JsonApiAdapter from 'ember-json-api/json-api-adapter';
|
2015-02-10 15:35:40 +08:00
|
|
|
|
|
|
|
import config from 'flarum/config/environment';
|
|
|
|
import AlertMessage from 'flarum/components/ui/alert-message';
|
2014-12-20 14:26:46 +08:00
|
|
|
|
2015-01-16 14:45:23 +08:00
|
|
|
export default JsonApiAdapter.extend({
|
2015-01-30 09:35:57 +08:00
|
|
|
host: config.apiURL,
|
|
|
|
|
2015-02-03 16:10:34 +08:00
|
|
|
ajaxError: function(jqXHR) {
|
|
|
|
var errors = this._super(jqXHR);
|
2015-02-10 15:35:40 +08:00
|
|
|
|
|
|
|
// 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 16:10:34 +08:00
|
|
|
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 15:35:40 +08:00
|
|
|
return new DS.InvalidError(newErrors);
|
2015-02-03 16:10:34 +08:00
|
|
|
}
|
2015-02-10 15:35:40 +08:00
|
|
|
|
|
|
|
// 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 12:04:07 +08:00
|
|
|
message: 'Something went wrong: '+errors.message.errors[0].code
|
2015-02-10 15:35:40 +08:00
|
|
|
});
|
|
|
|
this.get('alerts').send('alert', message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-03 16:10:34 +08:00
|
|
|
return errors;
|
2015-01-30 09:35:57 +08:00
|
|
|
}
|
2015-02-09 17:44:18 +08:00
|
|
|
});
|