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

Fixes https://github.com/elastic/kibana/issues/217433

## Summary

This PR fixes the Console output when the response body is an empty
string.

In https://github.com/elastic/kibana/pull/199975, we made Console to
display an `OK` output if the status code is 200 but response body is
`null`. Previously, `POST
/_cluster/voting_config_exclusions?node_names=node` returned `null` and
so the output was correctly set to `OK` but now the response is an empty
string and this case isn't covered correctly. In this PR, we make sure
to perform the right check at the right point so that both `null` and
empty strings are covered.

The test that covers this scenario and had failures:
7092e79157/src/platform/test/functional/apps/console/_console.ts (L258)


To run the failing test with the new Es snapshots:

```
ES_SNAPSHOT_MANIFEST="20250414-022022_f16f4ce6/manifest.json" node scripts/functional_tests_server.js --config ./src/platform/test/functional/apps/console/config.ts
```

and 

```
ES_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"
```
This commit is contained in:
Elena Stoeva 2025-04-14 23:06:14 +01:00 committed by GitHub
parent dbbc4751d3
commit 170651ac78
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);
@ -153,22 +158,18 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
}
} catch (error) {
let value;
const { response, body } = error as IHttpFetchError;
const { response } = 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 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,
// 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 && body === null) {
if (statusCode === 200) {
value = 'OK';
} 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) {