Ghost/ghost/admin/app/services/feature.js
Kevin Ansfield b4cdc85a59 "400 Version Mismatch" error handling
refs https://github.com/TryGhost/Ghost/issues/6949

Handle version mismatch errors by:
- displaying an alert asking the user to copy any data and refresh
- disabling navigation so that unsaved data is not accidentally lost

Detailed changes:
- add `error` action to application route for global route-based error handling
- remove 404-handler mixin, move logic into app route error handler
- update `.catch` in validation-engine so that promises are rejected with the
  original error objects
- add `VersionMismatchError` and `isVersionMismatchError` to ajax service
- add `upgrade-status` service
  - has a method to trigger the alert and toggle the "upgrade required" mode
  - is injected into all routes by default so that it can be checked before
    transitioning
- add `Route` override
  - updates the `willTransition` hook to check the `upgrade-status` service
    and abort the transition if we're in "upgrade required" mode
- update notifications `showAPIError` method to handle version mismatch errors
- update any areas where we were catching ajax errors manually so that the
  version mismatch error handling is obeyed
- fix redirect tests in editor acceptance test
- fix mirage's handling of 404s for unknown posts in get post requests
- adjust alert z-index to to appear above modal backgrounds
2016-07-08 14:56:26 +01:00

84 lines
2.3 KiB
JavaScript

import Ember from 'ember';
import Service from 'ember-service';
import computed from 'ember-computed';
import injectService from 'ember-service/inject';
import set from 'ember-metal/set';
// ember-cli-shims doesn't export Error
const {Error: EmberError} = Ember;
export function feature(name) {
return computed(`config.${name}`, `labs.${name}`, {
get() {
if (this.get(`config.${name}`)) {
return this.get(`config.${name}`);
}
return this.get(`labs.${name}`) || false;
},
set(key, value) {
this.update(key, value);
return value;
}
});
}
export default Service.extend({
store: injectService(),
config: injectService(),
notifications: injectService(),
publicAPI: feature('publicAPI'),
subscribers: feature('subscribers'),
internalTags: feature('internalTags'),
_settings: null,
labs: computed('_settings.labs', function () {
let labs = this.get('_settings.labs');
try {
return JSON.parse(labs) || {};
} catch (e) {
return {};
}
}),
fetch() {
return this.get('store').queryRecord('setting', {type: 'blog,theme,private'}).then((settings) => {
this.set('_settings', settings);
return true;
});
},
update(key, value) {
let settings = this.get('_settings');
let labs = this.get('labs');
// set the new labs key value
set(labs, key, value);
// update the 'labs' key of the settings model
settings.set('labs', JSON.stringify(labs));
return settings.save().then(() => {
// return the labs key value that we get from the server
this.notifyPropertyChange('labs');
return this.get(`labs.${key}`);
}).catch((error) => {
settings.rollbackAttributes();
this.notifyPropertyChange('labs');
// we'll always have an errors object unless we hit a
// validation error
if (!error) {
throw new EmberError(`Validation of the feature service settings model failed when updating labs.`);
}
this.get('notifications').showAPIError(error);
return this.get(`labs.${key}`);
});
}
});