Skip to content

Commit

Permalink
Fix http interceptor bug preventing 4xx/5xx responses from being caught
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Sep 11, 2020
1 parent e57009c commit 0e1f2eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,24 @@ describe('HttpLogic', () => {
describe('errorConnectingInterceptor', () => {
it('handles errors connecting to Enterprise Search', async () => {
const { responseError } = mockHttp.intercept.mock.calls[0][0] as any;
await responseError({ response: { url: '/api/app_search/engines', status: 502 } });
const httpResponse = { response: { url: '/api/app_search/engines', status: 502 } };
await expect(responseError(httpResponse)).rejects.toEqual(httpResponse);

expect(HttpLogic.actions.setErrorConnecting).toHaveBeenCalled();
});

it('does not handle non-502 Enterprise Search errors', async () => {
const { responseError } = mockHttp.intercept.mock.calls[0][0] as any;
await responseError({ response: { url: '/api/workplace_search/overview', status: 404 } });
const httpResponse = { response: { url: '/api/workplace_search/overview', status: 404 } };
await expect(responseError(httpResponse)).rejects.toEqual(httpResponse);

expect(HttpLogic.actions.setErrorConnecting).not.toHaveBeenCalled();
});

it('does not handle errors for unrelated calls', async () => {
const { responseError } = mockHttp.intercept.mock.calls[0][0] as any;
await responseError({ response: { url: '/api/some_other_plugin/', status: 502 } });
const httpResponse = { response: { url: '/api/some_other_plugin/', status: 502 } };
await expect(responseError(httpResponse)).rejects.toEqual(httpResponse);

expect(HttpLogic.actions.setErrorConnecting).not.toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export const HttpLogic = kea<MakeLogicType<IHttpValues, IHttpActions>>({
if (isApiResponse && hasErrorConnecting) {
actions.setErrorConnecting(true);
}
return httpResponse;

// Re-throw error so that downstream catches work as expected
return Promise.reject(httpResponse);
},
});
httpInterceptors.push(errorConnectingInterceptor);
Expand Down

0 comments on commit 0e1f2eb

Please sign in to comment.