Skip to content

Commit

Permalink
Handle 503 responses from the status API (#98738) (#98820)
Browse files Browse the repository at this point in the history
* Handle 503 responses from the status API

* PR feedback: additional test

Co-authored-by: Larry Gregory <[email protected]>
  • Loading branch information
kibanamachine and legrego authored Apr 29, 2021
1 parent 385cb54 commit 547018d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
45 changes: 45 additions & 0 deletions src/core/public/core_app/status/lib/load_status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,51 @@ describe('response processing', () => {
expect(data.name).toEqual('My computer');
});

test('throws when an error occurs', async () => {
http.get.mockReset();

http.get.mockRejectedValue(new Error());

await expect(loadStatus({ http, notifications })).rejects.toThrowError();
expect(notifications.toasts.addDanger).toHaveBeenCalledTimes(1);
});

test('throws when a 503 occurs which does not contain an appropriate payload', async () => {
const error = new Error() as any;
error.response = { status: 503 };
error.body = {};

http.get.mockReset();
http.get.mockRejectedValue(error);

await expect(loadStatus({ http, notifications })).rejects.toThrowError();
expect(notifications.toasts.addDanger).toHaveBeenCalledTimes(1);
});

test('does not throw when a 503 occurs which contains an appropriate payload', async () => {
const error = new Error() as any;
error.response = { status: 503 };
error.body = mockedResponse;

http.get.mockReset();
http.get.mockRejectedValue(error);

const data = await loadStatus({ http, notifications });
expect(data.name).toEqual('My computer');
});

test('throws when a non-503 occurs which contains an appropriate payload', async () => {
const error = new Error() as any;
error.response = { status: 500 };
error.body = mockedResponse;

http.get.mockReset();
http.get.mockRejectedValue(error);

await expect(loadStatus({ http, notifications })).rejects.toThrowError();
expect(notifications.toasts.addDanger).toHaveBeenCalledTimes(1);
});

test('includes the plugin statuses', async () => {
const data = await loadStatus({ http, notifications });
expect(data.statuses).toEqual([
Expand Down
37 changes: 24 additions & 13 deletions src/core/public/core_app/status/lib/load_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,32 @@ export async function loadStatus({
try {
response = await http.get('/api/status');
} catch (e) {
if ((e.response?.status ?? 0) >= 400) {
notifications.toasts.addDanger(
i18n.translate('core.statusPage.loadStatus.serverStatusCodeErrorMessage', {
defaultMessage: 'Failed to request server status with status code {responseStatus}',
values: { responseStatus: e.response?.status },
})
);
// API returns a 503 response if not all services are available.
// In this case, we want to treat this as a successful API call, so that we can
// display Kibana's status correctly.
// 503 responses can happen for other reasons (such as proxies), so we make an educated
// guess here to determine if the response payload looks like an appropriate `StatusResponse`.
const ignoreError = e.response?.status === 503 && typeof e.body?.name === 'string';

if (ignoreError) {
response = e.body;
} else {
notifications.toasts.addDanger(
i18n.translate('core.statusPage.loadStatus.serverIsDownErrorMessage', {
defaultMessage: 'Failed to request server status. Perhaps your server is down?',
})
);
if ((e.response?.status ?? 0) >= 400) {
notifications.toasts.addDanger(
i18n.translate('core.statusPage.loadStatus.serverStatusCodeErrorMessage', {
defaultMessage: 'Failed to request server status with status code {responseStatus}',
values: { responseStatus: e.response?.status },
})
);
} else {
notifications.toasts.addDanger(
i18n.translate('core.statusPage.loadStatus.serverIsDownErrorMessage', {
defaultMessage: 'Failed to request server status. Perhaps your server is down?',
})
);
}
throw e;
}
throw e;
}

return {
Expand Down

0 comments on commit 547018d

Please sign in to comment.