diff --git a/src/platform/plugins/shared/console/public/application/hooks/use_send_current_request/send_request.ts b/src/platform/plugins/shared/console/public/application/hooks/use_send_current_request/send_request.ts index b9f962e603d3c..5cc5c51e65b60 100644 --- a/src/platform/plugins/shared/console/public/application/hooks/use_send_current_request/send_request.ts +++ b/src/platform/plugins/shared/console/public/application/hooks/use_send_current_request/send_request.ts @@ -158,16 +158,14 @@ export function sendRequest(args: RequestArgs): Promise { } } catch (error) { let value; - const { response } = error as IHttpFetchError; + const { response, body: errorBody } = error as IHttpFetchError; const { statusCode, statusText } = extractStatusCodeAndText(response, path); - // When the request is sent, the HTTP library tries to parse the response body as JSON. - // However, if the response body is not in valid JSON format, it throws an error. - // To handle this, if the request resolves with a 200 status code but has an invalid body, - // we should still display a success message to the user. - if (statusCode === 200) { + if (statusCode === 200 && !errorBody) { value = 'OK'; + } else if (errorBody) { + value = JSON.stringify(errorBody, null, 2); } else { value = 'Request failed to get to the server (status code: ' + statusCode + ')'; } diff --git a/src/platform/test/functional/apps/console/_console.ts b/src/platform/test/functional/apps/console/_console.ts index b8b359f382260..2e696fef0a533 100644 --- a/src/platform/test/functional/apps/console/_console.ts +++ b/src/platform/test/functional/apps/console/_console.ts @@ -270,5 +270,25 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { expect(actualResponse).to.contain('OK'); }); }); + + it('Shows error body if HTTP request to server fails', async () => { + await PageObjects.console.clearEditorText(); + + // This request will return 200 but with an empty body + await PageObjects.console.enterText( + 'POST kbn:/api/alerting/rule/3603c386-9102-4c74-800d-2242e52bec98\n' + + '{\n' + + ' "name": "Alert on status change",\n' + + ' "rule_type_id": ".es-querya"\n' + + '}' + ); + await PageObjects.console.clickPlay(); + + await retry.try(async () => { + const actualResponse = await PageObjects.console.getOutputText(); + log.debug(actualResponse); + expect(actualResponse).to.contain('"statusCode": 400'); + }); + }); }); }