Ghost/ghost/admin/app/routes/setup/one.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

69 lines
1.7 KiB
JavaScript

import Ember from 'ember';
import Route from 'ember-route';
import injectService from 'ember-service/inject';
import EmberObject from 'ember-object';
import run from 'ember-runloop';
import AjaxService from 'ember-ajax/services/ajax';
// ember-cli-shims doesn't export Ember.testing
const {testing} = Ember;
let DownloadCountPoller = EmberObject.extend({
url: null,
count: '',
runId: null,
ajax: AjaxService.create(),
notifications: injectService(),
init() {
this._super(...arguments);
this.downloadCounter();
this.poll();
},
poll() {
let interval = testing ? 20 : 2000;
let runId = run.later(this, function () {
this.downloadCounter();
if (!testing) {
this.poll();
}
}, interval);
this.set('runId', runId);
},
downloadCounter() {
this.get('ajax').request(this.get('url')).then((data) => {
let pattern = /(-?\d+)(\d{3})/;
let count = data.count.toString();
while (pattern.test(count)) {
count = count.replace(pattern, '$1,$2');
}
this.set('count', count);
}).catch((error) => {
this.set('count', '');
this.get('notifications').showAPIError(error);
});
}
});
export default Route.extend({
ghostPaths: injectService(),
model() {
return DownloadCountPoller.create({url: this.get('ghostPaths.count')});
},
resetController(controller, isExiting) {
if (isExiting) {
run.cancel(controller.get('model.runId'));
controller.set('model', null);
}
}
});