[9.0] [Console] Fix output for empty response body (#218104) (#218185)

# Backport

This will backport the following commits from `main` to `9.0`:
- [[Console] Fix output for empty response body
(#218104)](https://github.com/elastic/kibana/pull/218104)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Elena
Stoeva","email":"59341489+ElenaStoeva@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-04-14T22:06:14Z","message":"[Console]
Fix output for empty response body (#218104)\n\nFixes
https://github.com/elastic/kibana/issues/217433\n\n## Summary\n\nThis PR
fixes the Console output when the response body is an
empty\nstring.\n\nIn https://github.com/elastic/kibana/pull/199975, we
made Console to\ndisplay an `OK` output if the status code is 200 but
response body is\n`null`. Previously,
`POST\n/_cluster/voting_config_exclusions?node_names=node` returned
`null` and\nso the output was correctly set to `OK` but now the response
is an empty\nstring and this case isn't covered correctly. In this PR,
we make sure\nto perform the right check at the right point so that both
`null` and\nempty strings are covered.\n\nThe test that covers this
scenario and had
failures:\n7092e79157/src/platform/test/functional/apps/console/_console.ts (L258)\n\n\nTo
run the failing test with the new Es
snapshots:\n\n```\nES_SNAPSHOT_MANIFEST=\"20250414-022022_f16f4ce6/manifest.json\"
node scripts/functional_tests_server.js --config
./src/platform/test/functional/apps/console/config.ts\n```\n\nand
\n\n```\nES_SNAPSHOT_MANIFEST=\"20250414-021844_4ed1a000/manifest.json\"
node scripts/functional_test_runner.js --config
./src/platform/test/functional/apps/console/config.ts --grep=\"Shows OK
when status code is 200 but body is
empty\"\n```","sha":"170651ac78595f6fe8e2ab4924fd5300b8a4f14e","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Console","Team:Kibana
Management","release_note:skip","backport:prev-minor","v9.1.0","v8.19.0"],"title":"[Console]
Fix output for empty response
body","number":218104,"url":"https://github.com/elastic/kibana/pull/218104","mergeCommit":{"message":"[Console]
Fix output for empty response body (#218104)\n\nFixes
https://github.com/elastic/kibana/issues/217433\n\n## Summary\n\nThis PR
fixes the Console output when the response body is an
empty\nstring.\n\nIn https://github.com/elastic/kibana/pull/199975, we
made Console to\ndisplay an `OK` output if the status code is 200 but
response body is\n`null`. Previously,
`POST\n/_cluster/voting_config_exclusions?node_names=node` returned
`null` and\nso the output was correctly set to `OK` but now the response
is an empty\nstring and this case isn't covered correctly. In this PR,
we make sure\nto perform the right check at the right point so that both
`null` and\nempty strings are covered.\n\nThe test that covers this
scenario and had
failures:\n7092e79157/src/platform/test/functional/apps/console/_console.ts (L258)\n\n\nTo
run the failing test with the new Es
snapshots:\n\n```\nES_SNAPSHOT_MANIFEST=\"20250414-022022_f16f4ce6/manifest.json\"
node scripts/functional_tests_server.js --config
./src/platform/test/functional/apps/console/config.ts\n```\n\nand
\n\n```\nES_SNAPSHOT_MANIFEST=\"20250414-021844_4ed1a000/manifest.json\"
node scripts/functional_test_runner.js --config
./src/platform/test/functional/apps/console/config.ts --grep=\"Shows OK
when status code is 200 but body is
empty\"\n```","sha":"170651ac78595f6fe8e2ab4924fd5300b8a4f14e"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/218104","number":218104,"mergeCommit":{"message":"[Console]
Fix output for empty response body (#218104)\n\nFixes
https://github.com/elastic/kibana/issues/217433\n\n## Summary\n\nThis PR
fixes the Console output when the response body is an
empty\nstring.\n\nIn https://github.com/elastic/kibana/pull/199975, we
made Console to\ndisplay an `OK` output if the status code is 200 but
response body is\n`null`. Previously,
`POST\n/_cluster/voting_config_exclusions?node_names=node` returned
`null` and\nso the output was correctly set to `OK` but now the response
is an empty\nstring and this case isn't covered correctly. In this PR,
we make sure\nto perform the right check at the right point so that both
`null` and\nempty strings are covered.\n\nThe test that covers this
scenario and had
failures:\n7092e79157/src/platform/test/functional/apps/console/_console.ts (L258)\n\n\nTo
run the failing test with the new Es
snapshots:\n\n```\nES_SNAPSHOT_MANIFEST=\"20250414-022022_f16f4ce6/manifest.json\"
node scripts/functional_tests_server.js --config
./src/platform/test/functional/apps/console/config.ts\n```\n\nand
\n\n```\nES_SNAPSHOT_MANIFEST=\"20250414-021844_4ed1a000/manifest.json\"
node scripts/functional_test_runner.js --config
./src/platform/test/functional/apps/console/config.ts --grep=\"Shows OK
when status code is 200 but body is
empty\"\n```","sha":"170651ac78595f6fe8e2ab4924fd5300b8a4f14e"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: Elena Stoeva <59341489+ElenaStoeva@users.noreply.github.com>
Co-authored-by: Elena Stoeva <elenastoeva99@gmail.com>
This commit is contained in:
Kibana Machine 2025-04-17 13:51:50 +02:00 committed by GitHub
parent b6bca1b6da
commit a7361cfc9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -115,8 +115,13 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
if (response) {
let value;
if (statusCode === 200 && !body) {
// If the request resolves with a 200 status code but has an empty or null body,
// we should still display a success message to the user.
value = 'OK';
}
// check if object is ArrayBuffer
if (body instanceof ArrayBuffer) {
else if (body instanceof ArrayBuffer) {
value = body;
} else {
value = typeof body === 'string' ? body : JSON.stringify(body, null, 2);
@ -157,18 +162,12 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
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 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) {
if (statusCode === 200 && !body) {
value = 'OK';
} else if (body) {
value = JSON.stringify(body, null, 2);
} else {
if (body) {
value = JSON.stringify(body, null, 2);
} else {
value = 'Request failed to get to the server (status code: ' + statusCode + ')';
}
value = 'Request failed to get to the server (status code: ' + statusCode + ')';
}
if (isMultiRequest) {