Merge pull request #6098 from acburdine/tag-404

Fix 404 error handling in editor, tags, and team routes
This commit is contained in:
Kevin Ansfield 2015-12-04 10:20:22 +00:00
commit d2ada96e4e
6 changed files with 73 additions and 4 deletions

View 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);
}
}
});

View File

@ -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) {

View File

@ -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);
}
});

View File

@ -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'],

View File

@ -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');
});
});
});
});
});

View 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;
}