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]' + }" + `); + } + }); });