Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #55 from Wikia/XW-3221
Browse files Browse the repository at this point in the history
XW-3221 | Handle not a valid wiki redirect
  • Loading branch information
Igor Rogatty authored Apr 7, 2017
2 parents b56b035 + f978ba4 commit f50d2cc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
29 changes: 29 additions & 0 deletions app/errors/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {defineError} from 'ember-exex/error';

const WikiVariablesFetchError = defineError({
name: 'WikiVariablesFetchError',
message: `Wiki variables couldn't be fetched`
});

const DesignSystemFetchError = defineError({
name: 'DesignSystemFetchError',
message: `Design System data couldn't be fetched`
});

const DontLogMeError = defineError({
name: 'DontLogMeError',
message: `Hack: this error was created only to stop executing Ember and redirect immediately`
});

const NonJsonApiResponseError = defineError({
name: 'NonJsonApiResponseError',
message: `The API response was in incorrect format`,
extends: DontLogMeError
});

export {
WikiVariablesFetchError,
DesignSystemFetchError,
DontLogMeError,
NonJsonApiResponseError
};
27 changes: 15 additions & 12 deletions app/models/wiki-variables.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import Ember from 'ember';
import {defineError} from 'ember-exex/error';
import fetch from '../utils/mediawiki-fetch';
import {buildUrl} from '../utils/url';
import {NonJsonApiResponseError, WikiVariablesFetchError, DesignSystemFetchError} from '../errors/main';

const WikiVariablesModel = Ember.Object.extend({});

const WikiVariablesFetchError = defineError({
name: 'WikiVariablesFetchError',
message: `Wiki variables couldn't be fetched`
});

const DesignSystemFetchError = defineError({
name: 'DesignSystemFetchError',
message: `Design System data couldn't be fetched`
});

WikiVariablesModel.reopenClass({
get(host) {
const url = buildUrl({
Expand All @@ -39,7 +29,16 @@ WikiVariablesModel.reopenClass({
});
}

return response.json();
const contentType = response.headers.get('content-type');

if (contentType && contentType.indexOf('application/json') !== -1) {
return response.json();
} else {
throw new NonJsonApiResponseError().withAdditionalData({
redirectLocation: 'http://community.wikia.com/wiki/Community_Central:Not_a_valid_community'
});
}

})
.then(({data}) => {
return fetch(
Expand Down Expand Up @@ -75,6 +74,10 @@ WikiVariablesModel.reopenClass({
});
})
.catch((error) => {
if (error.name === 'NonJsonApiResponseError') {
throw error;
}

throw new WikiVariablesFetchError({
code: error.code || 503
})
Expand Down
20 changes: 19 additions & 1 deletion app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import HeadTagsStaticMixin from '../mixins/head-tags-static';
import {normalizeToUnderscore} from '../utils/string';
import {track, trackActions} from '../utils/track';
import {getQueryString} from '../utils/url';
import {NonJsonApiResponseError, DontLogMeError} from '../errors/main';

import {disableCache, setResponseCaching, CachingInterval, CachingPolicy} from '../utils/fastboot-caching';

Expand Down Expand Up @@ -55,6 +56,20 @@ export default Route.extend(
return wikiVariablesModel;
})
.catch((error) => {
if (error instanceof NonJsonApiResponseError) {
const fastboot = this.get('fastboot');

fastboot.get('response.headers').set(
'location',
error.additionalData[0].redirectLocation
);
fastboot.set('response.statusCode', 301);

// TODO XW-3198
// We throw error to stop Ember
throw error;
}

this.injectScriptsFastbootOnly(null, transition.queryParams);
throw error;
});
Expand Down Expand Up @@ -149,7 +164,10 @@ export default Route.extend(
`${model.basePath}${fastbootRequest.get('path')}${getQueryString(fastbootRequest.get('queryParams'))}`
);
fastboot.set('response.statusCode', 301);
return false;

// TODO XW-3198
// We throw error to stop Ember and redirect immediately
throw new DontLogMeError();
}
},

Expand Down
17 changes: 12 additions & 5 deletions app/services/fastboot-error-consumer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Ember from 'ember';
import BaseConsumer from 'ember-error-handler/consumer/base-consumer';
import extend from '../utils/extend';
import {DontLogMeError} from '../errors/main';

const {inject} = Ember;

Expand All @@ -11,6 +12,17 @@ export default BaseConsumer.extend({
const fastboot = this.get('fastboot');

if (fastboot.get('isFastBoot')) {
// TODO XW-3198
// Don't log special type of errors. Currently we use them hack Ember and stop executing application
if (descriptor.get('error') instanceof DontLogMeError) {
return true;
}

const error = extend({}, descriptor.get('error'));
const errorWithStack = extend(error, {
stack: descriptor.get('normalizedStack')
});

const bunyan = FastBoot.require('bunyan');
const BunyanSyslog = FastBoot.require('bunyan-syslog');
// TODO we probably shouldn't create new instance on every error
Expand All @@ -28,11 +40,6 @@ export default BaseConsumer.extend({
}]
});

const error = extend({}, descriptor.get('error'));
const errorWithStack = extend(error, {
stack: descriptor.get('normalizedStack')
});

logger.error(
errorWithStack,
`FastBoot error: ${descriptor.get('normalizedMessage')}`
Expand Down

0 comments on commit f50d2cc

Please sign in to comment.