mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-27 15:30:37 +05:30
#302: Fix for dispatching bsod for 5xx
This commit is contained in:
parent
b0cc803043
commit
4c812cafda
16
src/components/ui/bsod/BsodMiddleware.js
Normal file
16
src/components/ui/bsod/BsodMiddleware.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
export default function BsodMiddleware(dispatchBsod, logger) {
|
||||||
|
return {
|
||||||
|
catch(resp) {
|
||||||
|
if (resp
|
||||||
|
&& resp.originalResponse
|
||||||
|
&& /404|5\d\d/.test(resp.originalResponse.status)
|
||||||
|
) {
|
||||||
|
dispatchBsod();
|
||||||
|
|
||||||
|
logger.warn('Unexpected response', {resp});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.reject(resp);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -2,23 +2,11 @@ import request from 'services/request';
|
|||||||
import logger from 'services/logger';
|
import logger from 'services/logger';
|
||||||
|
|
||||||
import dispatchBsod, { inject } from './dispatchBsod';
|
import dispatchBsod, { inject } from './dispatchBsod';
|
||||||
|
import BsodMiddleware from './BsodMiddleware';
|
||||||
|
|
||||||
export default function factory(store, stopLoading) {
|
export default function factory(store, stopLoading) {
|
||||||
inject(store, stopLoading);
|
inject(store, stopLoading);
|
||||||
|
|
||||||
// do bsod for 500/404 errors
|
// do bsod for 500/404 errors
|
||||||
request.addMiddleware({
|
request.addMiddleware(new BsodMiddleware(dispatchBsod, logger));
|
||||||
catch(resp) {
|
|
||||||
if (resp
|
|
||||||
&& resp.originalResponse
|
|
||||||
&& [5, 404].indexOf(resp.originalResponse.status) === 0
|
|
||||||
) {
|
|
||||||
dispatchBsod();
|
|
||||||
|
|
||||||
logger.warn('Unexpected response', {resp});
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.reject(resp);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
45
tests/components/ui/bsod/BsodMiddleware.test.js
Normal file
45
tests/components/ui/bsod/BsodMiddleware.test.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import expect from 'unexpected';
|
||||||
|
import sinon from 'sinon';
|
||||||
|
|
||||||
|
import BsodMiddleware from 'components/ui/bsod/BsodMiddleware';
|
||||||
|
|
||||||
|
describe('BsodMiddleware', () => {
|
||||||
|
[404, 500, 503, 555].forEach((code) =>
|
||||||
|
it(`should dispatch for ${code}`, () => {
|
||||||
|
const resp = {
|
||||||
|
originalResponse: {status: code}
|
||||||
|
};
|
||||||
|
|
||||||
|
const dispatch = sinon.spy();
|
||||||
|
const logger = {warn: sinon.spy()};
|
||||||
|
|
||||||
|
const middleware = new BsodMiddleware(dispatch, logger);
|
||||||
|
|
||||||
|
return expect(middleware.catch(resp), 'to be rejected with', resp)
|
||||||
|
.then(() => {
|
||||||
|
expect(dispatch, 'was called');
|
||||||
|
expect(logger.warn, 'to have a call satisfying', [
|
||||||
|
'Unexpected response',
|
||||||
|
{resp}
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
it('should not dispatch for 200', () => {
|
||||||
|
const resp = {
|
||||||
|
originalResponse: {status: 200}
|
||||||
|
};
|
||||||
|
|
||||||
|
const dispatch = sinon.spy();
|
||||||
|
const logger = {warn: sinon.spy()};
|
||||||
|
|
||||||
|
const middleware = new BsodMiddleware(dispatch, logger);
|
||||||
|
|
||||||
|
return expect(middleware.catch(resp), 'to be rejected with', resp)
|
||||||
|
.then(() => {
|
||||||
|
expect(dispatch, 'was not called');
|
||||||
|
expect(logger.warn, 'was not called');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user