From 2128958a5a5e3835eafbfd3adac9e47a25d38aca Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Tue, 15 Sep 2020 19:59:40 -0700 Subject: [PATCH] Add fallback for http errors without an errors array - all http error messages should still be expected to contain a message key, so should be a reliable fallback --- .../flash_messages/handle_api_errors.test.ts | 14 ++++++++++++++ .../shared/flash_messages/handle_api_errors.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.test.ts b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.test.ts index 6f2aec757eb37..c30631329b79d 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.test.ts @@ -52,6 +52,20 @@ describe('flashAPIErrors', () => { ]); }); + it('falls back to the basic message for http responses without an errors array', () => { + flashAPIErrors({ + body: { + statusCode: 404, + error: 'Not Found', + message: 'Not Found', + }, + } as any); + + expect(FlashMessagesLogic.actions.setFlashMessages).toHaveBeenCalledWith([ + { type: 'error', message: 'Not Found' }, + ]); + }); + it('displays a generic error message and re-throws non-API errors', () => { try { flashAPIErrors(Error('whatever') as any); diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.ts b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.ts index 5e56c4fb0bd22..2bd04d1d87f7d 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/handle_api_errors.ts @@ -40,7 +40,7 @@ export const flashAPIErrors = ( const errorFlashMessages: IFlashMessage[] = Array.isArray(error?.body?.attributes?.errors) ? error.body!.attributes.errors.map((message) => ({ type: 'error', message })) - : [{ type: 'error', message: defaultErrorMessage }]; + : [{ type: 'error', message: error?.body?.message || defaultErrorMessage }]; if (isQueued) { FlashMessagesLogic.actions.setQueuedMessages(errorFlashMessages);