From d9499fe57d6a5ca749c41caf5c9a292746a486d9 Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Wed, 13 Nov 2024 11:59:04 +0100 Subject: [PATCH 1/3] Show OK when body is null but status code is 200 --- .../hooks/use_send_current_request/send_request.ts | 14 +++++++++++--- test/functional/apps/console/_console.ts | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) 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 40f440eccd7ce..a30f03ce57721 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 @@ -157,10 +157,18 @@ export function sendRequest(args: RequestArgs): Promise { const { statusCode, statusText } = extractStatusCodeAndText(response, path); - if (body) { - value = JSON.stringify(body, null, 2); + // When the request is sent, the HTTP library tries to parse the response body as JSON. + // However, if the response body is empty or not in valid JSON format, it throws an error. + // To handle this, if the request resolves with a 200 status code but has an empty or invalid body, + // we should still display a success message to the user. + if (statusCode === 200 && body === null) { + value = 'OK'; } else { - value = 'Request failed to get to the server (status code: ' + statusCode + ')'; + if (body) { + value = JSON.stringify(body, null, 2); + } else { + value = 'Request failed to get to the server (status code: ' + statusCode + ')'; + } } if (isMultiRequest) { diff --git a/test/functional/apps/console/_console.ts b/test/functional/apps/console/_console.ts index 27339c408de85..eabb47ec01463 100644 --- a/test/functional/apps/console/_console.ts +++ b/test/functional/apps/console/_console.ts @@ -223,5 +223,19 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { expect(await PageObjects.console.hasSuccessBadge()).to.be(true); }); }); + + it('Shows OK when status code is 200 but body is empty', async () => { + await PageObjects.console.clearEditorText(); + + // This request will return 200 but with an empty body + await PageObjects.console.enterText('POST /_cluster/voting_config_exclusions?node_names=node'); + await PageObjects.console.clickPlay(); + + await retry.try(async () => { + const actualResponse = await PageObjects.console.getOutputText(); + log.debug(actualResponse); + expect(actualResponse).to.contain('OK'); + }); + }); }); } From d0161ae9883847135709bd3b3cc7e12603fd9f4e Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Wed, 13 Nov 2024 11:59:13 +0100 Subject: [PATCH 2/3] commit with @elastic email From d68feba22c32aa693ae2cba020ca142550142e22 Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Wed, 13 Nov 2024 11:59:59 +0100 Subject: [PATCH 3/3] FIx linter --- test/functional/apps/console/_console.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/functional/apps/console/_console.ts b/test/functional/apps/console/_console.ts index eabb47ec01463..e53563542345d 100644 --- a/test/functional/apps/console/_console.ts +++ b/test/functional/apps/console/_console.ts @@ -228,7 +228,9 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.console.clearEditorText(); // This request will return 200 but with an empty body - await PageObjects.console.enterText('POST /_cluster/voting_config_exclusions?node_names=node'); + await PageObjects.console.enterText( + 'POST /_cluster/voting_config_exclusions?node_names=node' + ); await PageObjects.console.clickPlay(); await retry.try(async () => {