[Console] Handle multiple request exceptions (#129443)

* Add handling multiple request exceptions

- Fix code styling for send_request.test.ts
- Extract a way of getting content-type to a separate function

Co-authored-by: Muhammad Ibragimov <muhammad.ibragimov@elastic.co>
This commit is contained in:
Muhammad Ibragimov 2022-04-27 15:46:57 +05:00 committed by GitHub
parent f04d75f26e
commit eaf47aeb1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 30 deletions

View file

@ -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,
},
},
},
]);
{
response: {
statusCode: 400,
},
},
]);
const args = {
http: mockContextValue.services.http,
requests: [
{ method: 'GET', url: 'test-1', data: [] },
{ method: 'GET', url: 'test-2', data: [] },
],
};
const results = await sendRequest(args);
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 [firstRequest, secondRequest] = results;
expect(firstRequest.response.statusCode).toEqual(200);
expect(secondRequest.response.statusCode).toEqual(200);
expect(mockedSendRequest).toHaveBeenCalledWith(args);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
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 () => {

View file

@ -33,6 +33,9 @@ export interface RequestResult<V = unknown> {
response: ResponseObject<V>;
}
const getContentType = (response: Response | undefined) =>
(response?.headers.get('Content-Type') as BaseResponseType) ?? '';
let CURRENT_REQ_ID = 0;
export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
const requests = args.requests.slice();
@ -111,7 +114,7 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
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<RequestResult[]> {
} 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<RequestResult[]> {
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<RequestResult[]> {
method,
path,
},
});
};
// Reject on unknown errors
if (!response) {
reject(result);
}
// Add error to the list of results
results.push(result);
await sendNextRequest();
}
};