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) {
|
2015-02-26 12:48:23 +10:30
|
|
|
var message;
|
|
|
|
if (errors.status === 401) {
|
|
|
|
message = 'You don\'t have permission to do this.';
|
|
|
|
} else {
|
|
|
|
message = errors.message;
|
|
|
|
}
|
2015-02-26 13:28:44 +10:30
|
|
|
var alert = AlertMessage.extend({
|
2015-02-10 18:05:40 +10:30
|
|
|
type: 'warning',
|
2015-02-26 12:48:23 +10:30
|
|
|
message: message
|
2015-02-10 18:05:40 +10:30
|
|
|
});
|
2015-02-26 12:48:23 +10:30
|
|
|
this.get('alerts').send('alert', alert);
|
2015-02-10 18:05:40 +10:30
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|