From 91432051b1ea1cbc94f782b65b072a87dd8476ca Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 15 Nov 2023 12:40:13 -0500 Subject: [PATCH] [8.11] Add custom `inspect` representation for ES client's errors (#171304) (#171327) # Backport This will backport the following commits from `main` to `8.11`: - [Add custom `inspect` representation for ES client's errors (#171304)](https://github.com/elastic/kibana/pull/171304) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Pierre Gayvallet --- .../src/patch_client.ts | 7 ++++++ .../elasticsearch/errors.test.ts | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/packages/core/elasticsearch/core-elasticsearch-client-server-internal/src/patch_client.ts b/packages/core/elasticsearch/core-elasticsearch-client-server-internal/src/patch_client.ts index 75ea5db91b2e3..a7bdf3788d2c8 100644 --- a/packages/core/elasticsearch/core-elasticsearch-client-server-internal/src/patch_client.ts +++ b/packages/core/elasticsearch/core-elasticsearch-client-server-internal/src/patch_client.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import { inspect } from 'util'; import { errors } from '@elastic/elasticsearch'; export const patchElasticsearchClient = () => { @@ -17,4 +18,10 @@ export const patchElasticsearchClient = () => { message: this.message, }; }; + + // @ts-expect-error + baseErrorPrototype[inspect.custom] = function () { + // @ts-expect-error + return this.toJSON(); + }; }; diff --git a/src/core/server/integration_tests/elasticsearch/errors.test.ts b/src/core/server/integration_tests/elasticsearch/errors.test.ts index b9dcfb33a23a4..abefdc7bf356c 100644 --- a/src/core/server/integration_tests/elasticsearch/errors.test.ts +++ b/src/core/server/integration_tests/elasticsearch/errors.test.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import { inspect } from 'util'; import { createTestServers, type TestElasticsearchUtils, @@ -67,4 +68,28 @@ describe('elasticsearch clients errors', () => { `); } }); + + it('has the proper inspect representation', async () => { + const esClient = kibanaServer.coreStart.elasticsearch.client.asInternalUser; + + try { + await esClient.search({ + index: '.kibana', + // @ts-expect-error yes this is invalid + query: { someInvalidQuery: { foo: 'bar' } }, + }); + expect('should have thrown').toEqual('but it did not'); + } catch (e) { + expect(inspect(e)).toMatchInlineSnapshot(` + "{ + name: 'ResponseError', + message: 'parsing_exception\\\\n' + + '\\\\tCaused by:\\\\n' + + '\\\\t\\\\tnamed_object_not_found_exception: [1:30] unknown field [someInvalidQuery]\\\\n' + + '\\\\tRoot causes:\\\\n' + + '\\\\t\\\\tparsing_exception: unknown query [someInvalidQuery]' + }" + `); + } + }); });