From b9a1e0e2e5dd3658a5f40aa8eea90059b8f5fb12 Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Wed, 12 Feb 2020 09:58:30 +1300 Subject: [PATCH] handle empty response from state api in ui --- .../public/application/lib/alert_api.test.ts | 8 ++++++++ .../public/application/lib/alert_api.ts | 20 +++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.test.ts index 99838157abfdd..9c6f4daccc705 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.test.ts @@ -133,6 +133,14 @@ describe('loadAlertState', () => { }); expect(http.get).toHaveBeenCalledWith(`/api/alert/${alertId}/state`); }); + + test('should handle empty response from api', async () => { + const alertId = uuid.v4(); + http.get.mockResolvedValueOnce(''); + + expect(await loadAlertState({ http, alertId })).toEqual({}); + expect(http.get).toHaveBeenCalledWith(`/api/alert/${alertId}/state`); + }); }); describe('loadAlerts', () => { diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.ts index f3a12615d5303..22fd01c1aee81 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/alert_api.ts @@ -26,6 +26,7 @@ export async function loadAlert({ return await http.get(`${BASE_ALERT_API_PATH}/${alertId}`); } +type EmptyHttpResponse = ''; export async function loadAlertState({ http, alertId, @@ -33,14 +34,17 @@ export async function loadAlertState({ http: HttpSetup; alertId: string; }): Promise { - return await http.get(`${BASE_ALERT_API_PATH}/${alertId}/state`).then((state: AlertTaskState) => { - return pipe( - alertStateSchema.decode(state), - fold((e: t.Errors) => { - throw new Error(`Alert "${alertId}" has invalid state`); - }, t.identity) - ); - }); + return await http + .get(`${BASE_ALERT_API_PATH}/${alertId}/state`) + .then((state: AlertTaskState | EmptyHttpResponse) => (state ? state : {})) + .then((state: AlertTaskState) => { + return pipe( + alertStateSchema.decode(state), + fold((e: t.Errors) => { + throw new Error(`Alert "${alertId}" has invalid state`); + }, t.identity) + ); + }); } export async function loadAlerts({