Ghost/ghost/admin/app/adapters/base.js
Kevin Ansfield eb2a0359cf Refactor error handling
closes https://github.com/TryGhost/Ghost/issues/6974
- update "change password" fields/process to use inline validations
- remove `notifications.showErrors` and update all uses of it to `showAPIError`
- display multiple API errors as alerts rather than toaster notifications
- refactor `notifications.showAPIError`
  - remove `notifications.showErrors`, use a loop in `showAPIError` instead
  - properly determine the message from `AjaxError` or `AdapterError` objects
  - determine a unique key if possible so that we don't lose multiple different alerts
- add `ServerUnreachable` error for when we get a status code of 0 (eg, when the ghost service has been shut down)
- simplify error messages for our custom ajax errors
2016-07-08 15:16:54 +01:00

55 lines
1.3 KiB
JavaScript

import injectService from 'ember-service/inject';
import RESTAdapter from 'ember-data/adapters/rest';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
import config from 'ghost-admin/config/environment';
export default RESTAdapter.extend(DataAdapterMixin, {
authorizer: 'authorizer:oauth2',
host: window.location.origin,
namespace: ghostPaths().apiRoot.slice(1),
session: injectService(),
headers: {
'X-Ghost-Version': config.APP.version
},
shouldBackgroundReloadRecord() {
return false;
},
query(store, type, query) {
let id;
if (query.id) {
id = query.id;
delete query.id;
}
return this.ajax(this.buildURL(type.modelName, id), 'GET', {data: query});
},
buildURL() {
// Ensure trailing slashes
let url = this._super(...arguments);
if (url.slice(-1) !== '/') {
url += '/';
}
return url;
},
handleResponse(status) {
if (status === 401) {
if (this.get('session.isAuthenticated')) {
this.get('session').invalidate();
}
}
return this._super(...arguments);
}
});