Skip to content

Commit

Permalink
handle empty response from state api in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Feb 11, 2020
1 parent 059dcab commit b9a1e0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ export async function loadAlert({
return await http.get(`${BASE_ALERT_API_PATH}/${alertId}`);
}

type EmptyHttpResponse = '';
export async function loadAlertState({
http,
alertId,
}: {
http: HttpSetup;
alertId: string;
}): Promise<AlertTaskState> {
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({
Expand Down

0 comments on commit b9a1e0e

Please sign in to comment.