mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 03:14:03 +03:00
Merge pull request #6098 from acburdine/tag-404
Fix 404 error handling in editor, tags, and team routes
This commit is contained in:
commit
d2ada96e4e
23
ghost/admin/app/mixins/404-handler.js
Normal file
23
ghost/admin/app/mixins/404-handler.js
Normal file
@ -0,0 +1,23 @@
|
||||
import Ember from 'ember';
|
||||
|
||||
export default Ember.Mixin.create({
|
||||
actions: {
|
||||
error(error, transition) {
|
||||
if (error.errors[0].errorType === 'NotFoundError') {
|
||||
transition.abort();
|
||||
|
||||
let routeInfo = transition.handlerInfos[transition.handlerInfos.length - 1];
|
||||
let router = this.get('router');
|
||||
let params = [];
|
||||
|
||||
for (const key of Object.keys(routeInfo.params)) {
|
||||
params.push(routeInfo.params[key]);
|
||||
}
|
||||
|
||||
return this.transitionTo('error404', router.generate(routeInfo.name, ...params).replace('/ghost/', '').replace(/^\//g, ''));
|
||||
}
|
||||
|
||||
return this._super(...arguments);
|
||||
}
|
||||
}
|
||||
});
|
@ -1,10 +1,11 @@
|
||||
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
||||
import AuthenticatedRoute from 'ghost/routes/authenticated';
|
||||
import base from 'ghost/mixins/editor-base-route';
|
||||
import NotFoundHandler from 'ghost/mixins/404-handler';
|
||||
import isNumber from 'ghost/utils/isNumber';
|
||||
import isFinite from 'ghost/utils/isFinite';
|
||||
|
||||
export default AuthenticatedRoute.extend(base, {
|
||||
export default AuthenticatedRoute.extend(base, NotFoundHandler, {
|
||||
titleToken: 'Editor',
|
||||
|
||||
beforeModel(transition) {
|
||||
|
@ -1,7 +1,8 @@
|
||||
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
||||
import AuthenticatedRoute from 'ghost/routes/authenticated';
|
||||
import NotFoundHandler from 'ghost/mixins/404-handler';
|
||||
|
||||
export default AuthenticatedRoute.extend({
|
||||
export default AuthenticatedRoute.extend(NotFoundHandler, {
|
||||
|
||||
model(params) {
|
||||
return this.store.queryRecord('tag', {slug: params.tag_slug});
|
||||
@ -16,5 +17,4 @@ export default AuthenticatedRoute.extend({
|
||||
this._super(...arguments);
|
||||
this.set('controller.model', null);
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -2,8 +2,9 @@
|
||||
import AuthenticatedRoute from 'ghost/routes/authenticated';
|
||||
import CurrentUserSettings from 'ghost/mixins/current-user-settings';
|
||||
import styleBody from 'ghost/mixins/style-body';
|
||||
import NotFoundHandler from 'ghost/mixins/404-handler';
|
||||
|
||||
export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, {
|
||||
export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, NotFoundHandler, {
|
||||
titleToken: 'Team - User',
|
||||
|
||||
classNames: ['team-view-user'],
|
||||
|
@ -11,6 +11,8 @@ import Ember from 'ember';
|
||||
import startApp from '../../helpers/start-app';
|
||||
import destroyApp from '../../helpers/destroy-app';
|
||||
import { invalidateSession, authenticateSession } from 'ghost/tests/helpers/ember-simple-auth';
|
||||
import { errorOverride, errorReset } from 'ghost/tests/helpers/adapter-error';
|
||||
import Mirage from 'ember-cli-mirage';
|
||||
|
||||
const {run} = Ember;
|
||||
|
||||
@ -279,5 +281,28 @@ describe('Acceptance: Settings - Tags', function () {
|
||||
.to.equal(32);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with 404', function () {
|
||||
beforeEach(function () {
|
||||
errorOverride();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
errorReset();
|
||||
});
|
||||
|
||||
it('redirects to 404 when tag does not exist', function () {
|
||||
server.get('/tags/slug/unknown/', function () {
|
||||
return new Mirage.Response(404, {'Content-Type': 'application/json'}, {errors: [{message: 'Tag not found.', errorType: 'NotFoundError'}]});
|
||||
});
|
||||
|
||||
visit('settings/tags/unknown');
|
||||
|
||||
andThen(() => {
|
||||
expect(currentPath()).to.equal('error404');
|
||||
expect(currentURL()).to.equal('/settings/tags/unknown');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
19
ghost/admin/tests/helpers/adapter-error.js
Normal file
19
ghost/admin/tests/helpers/adapter-error.js
Normal file
@ -0,0 +1,19 @@
|
||||
import Ember from 'ember';
|
||||
|
||||
// This is needed for testing error responses in acceptance tests
|
||||
// See http://williamsbdev.com/posts/testing-rsvp-errors-handled-globally/
|
||||
|
||||
let originalException;
|
||||
let originalLoggerError;
|
||||
|
||||
export function errorOverride() {
|
||||
originalException = Ember.Test.adapter.exception;
|
||||
originalLoggerError = Ember.Logger.error;
|
||||
Ember.Test.adapter.exception = function () {};
|
||||
Ember.Logger.error = function () {};
|
||||
}
|
||||
|
||||
export function errorReset() {
|
||||
Ember.Test.adapter.exception = originalException;
|
||||
Ember.Logger.error = originalLoggerError;
|
||||
}
|
Loading…
Reference in New Issue
Block a user