Skip to content

Commit

Permalink
Add custom inspect representation for ES client's errors (elastic#1…
Browse files Browse the repository at this point in the history
…71304)

Follow-up of elastic#171018
  • Loading branch information
pgayvallet authored Nov 15, 2023
1 parent 74a5fba commit 11eb73e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { inspect } from 'util';
import { errors } from '@elastic/elasticsearch';

export const patchElasticsearchClient = () => {
Expand All @@ -17,4 +18,10 @@ export const patchElasticsearchClient = () => {
message: this.message,
};
};

// @ts-expect-error
baseErrorPrototype[inspect.custom] = function () {
// @ts-expect-error
return this.toJSON();
};
};
25 changes: 25 additions & 0 deletions src/core/server/integration_tests/elasticsearch/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { inspect } from 'util';
import {
createTestServers,
type TestElasticsearchUtils,
Expand Down Expand Up @@ -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]'
}"
`);
}
});
});

0 comments on commit 11eb73e

Please sign in to comment.