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); diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts index 74e233ad6b320..c03954179184e 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts @@ -12,3 +12,4 @@ export { IFlashMessagesActions, } from './flash_messages_logic'; export { FlashMessagesProvider } from './flash_messages_provider'; +export { flashAPIErrors } from './handle_api_errors';