Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 503 responses from the status API #98738

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 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,39 @@ 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;
legrego marked this conversation as resolved.
Show resolved Hide resolved

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

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

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