diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts index 60ced085c6891..841633cded51e 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts @@ -49,34 +49,43 @@ describe('sendRequest', () => { expect(mockedSendRequest).toHaveBeenCalledTimes(1); }); - it('should send multiple requests', async () => { - mockedSendRequest.mockResolvedValue([ - { - response: { - statusCode: 200, + describe('with multiple requests', () => { + it('should return results with exceptions', async () => { + mockedSendRequest.mockResolvedValue([ + { + response: { + statusCode: 200, + }, }, - }, - { - response: { - statusCode: 200, + { + response: { + statusCode: 200, + }, }, - }, - ]); - - const args = { - http: mockContextValue.services.http, - requests: [ - { method: 'GET', url: 'test-1', data: [] }, - { method: 'GET', url: 'test-2', data: [] }, - ], - }; - const results = await sendRequest(args); + { + response: { + statusCode: 400, + }, + }, + ]); - const [firstRequest, secondRequest] = results; - expect(firstRequest.response.statusCode).toEqual(200); - expect(secondRequest.response.statusCode).toEqual(200); - expect(mockedSendRequest).toHaveBeenCalledWith(args); - expect(mockedSendRequest).toHaveBeenCalledTimes(1); + const args = { + http: mockContextValue.services.http, + requests: [ + { method: 'GET', url: 'success', data: [] }, + { method: 'GET', url: 'success', data: [] }, + { method: 'GET', url: 'fail', data: [] }, + ], + }; + const results = await sendRequest(args); + + const [firstCall, secondCall, thirdCall] = results; + expect(firstCall.response.statusCode).toEqual(200); + expect(secondCall.response.statusCode).toEqual(200); + expect(thirdCall.response.statusCode).toEqual(400); + expect(mockedSendRequest).toHaveBeenCalledWith(args); + expect(mockedSendRequest).toHaveBeenCalledTimes(1); + }); }); it('should handle errors', async () => { diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts index 1247f3f78aa68..1ac47df30fca5 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts @@ -33,6 +33,9 @@ export interface RequestResult { response: ResponseObject; } +const getContentType = (response: Response | undefined) => + (response?.headers.get('Content-Type') as BaseResponseType) ?? ''; + let CURRENT_REQ_ID = 0; export function sendRequest(args: RequestArgs): Promise { const requests = args.requests.slice(); @@ -111,7 +114,7 @@ export function sendRequest(args: RequestArgs): Promise { timeMs: Date.now() - startTime, statusCode: response.status, statusText: response.statusText, - contentType: response.headers.get('Content-Type') as BaseResponseType, + contentType: getContentType(response), value, }, request: { @@ -128,9 +131,8 @@ export function sendRequest(args: RequestArgs): Promise { } catch (error) { let value; const { response, body } = error as IHttpFetchError; - const contentType = response?.headers.get('Content-Type') ?? ''; const statusCode = response?.status ?? 500; - const statusText = error?.response?.statusText ?? 'error'; + const statusText = response?.statusText ?? 'error'; if (body) { value = JSON.stringify(body, null, 2); @@ -142,10 +144,10 @@ export function sendRequest(args: RequestArgs): Promise { value = '# ' + req.method + ' ' + req.url + '\n' + value; } - reject({ + const result = { response: { value, - contentType, + contentType: getContentType(response), timeMs: Date.now() - startTime, statusCode, statusText, @@ -155,7 +157,16 @@ export function sendRequest(args: RequestArgs): Promise { method, path, }, - }); + }; + + // Reject on unknown errors + if (!response) { + reject(result); + } + + // Add error to the list of results + results.push(result); + await sendNextRequest(); } };