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 b04c8aff772f1..45fc6749e3426 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 @@ -5,7 +5,12 @@ */ jest.mock('./', () => ({ - FlashMessagesLogic: { actions: { setFlashMessages: jest.fn() } }, + FlashMessagesLogic: { + actions: { + setFlashMessages: jest.fn(), + setQueuedMessages: jest.fn(), + }, + }, })); import { FlashMessagesLogic } from './'; @@ -37,6 +42,16 @@ describe('handleAPIError', () => { ]); }); + it('queues messages when isQueued is passed', () => { + handleAPIError(mockHttpError, { isQueued: true }); + + expect(FlashMessagesLogic.actions.setQueuedMessages).toHaveBeenCalledWith([ + { type: 'error', message: 'Could not find X' }, + { type: 'error', message: 'Could not find Y' }, + { type: 'error', message: 'Something else bad happened' }, + ]); + }); + it('displays a generic error message and re-throws non-API errors', () => { try { handleAPIError(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 52744d67bf36c..9089aba147da2 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 @@ -25,18 +25,28 @@ interface IErrorResponse { errors: string[]; }; } +interface IOptions { + isQueued?: boolean; +} /** * Converts API/HTTP errors into user-facing Flash Messages */ -export const handleAPIError = (error: HttpResponse) => { +export const handleAPIError = ( + error: HttpResponse, + { isQueued }: IOptions = {} +) => { const defaultErrorMessage = 'An unexpected error occurred'; const errorFlashMessages: IFlashMessage[] = Array.isArray(error?.body?.attributes?.errors) ? error.body!.attributes.errors.map((message) => ({ type: 'error', message })) : [{ type: 'error', message: defaultErrorMessage }]; - FlashMessagesLogic.actions.setFlashMessages(errorFlashMessages); + if (isQueued) { + FlashMessagesLogic.actions.setQueuedMessages(errorFlashMessages); + } else { + FlashMessagesLogic.actions.setFlashMessages(errorFlashMessages); + } // If this was a programming error or a failed request (such as a CORS) error, // we rethrow the error so it shows up in the developer console