Skip to content

Commit

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

# Backport

This will backport the following commits from `main` to `8.11`:
- [Add custom `inspect` representation for ES client's errors
(#171304)](#171304)

<!--- Backport version: 8.9.7 -->

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

<!--BACKPORT [{"author":{"name":"Pierre
Gayvallet","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-11-15T16:20:45Z","message":"Add
custom `inspect` representation for ES client's errors
(#171304)\n\nFollow-up of
#171018","sha":"11eb73ee1d734ecdcbbbdbae63b859b5b3795a4d","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:prev-minor","backport:prev-MAJOR","v8.12.0"],"number":171304,"url":"https://github.com/elastic/kibana/pull/171304","mergeCommit":{"message":"Add
custom `inspect` representation for ES client's errors
(#171304)\n\nFollow-up of
#171018","sha":"11eb73ee1d734ecdcbbbdbae63b859b5b3795a4d"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/171304","number":171304,"mergeCommit":{"message":"Add
custom `inspect` representation for ES client's errors
(#171304)\n\nFollow-up of
#171018","sha":"11eb73ee1d734ecdcbbbdbae63b859b5b3795a4d"}}]}]
BACKPORT-->

Co-authored-by: Pierre Gayvallet <[email protected]>
  • Loading branch information
kibanamachine and pgayvallet authored Nov 15, 2023
1 parent ce6db06 commit 9143205
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 9143205

Please sign in to comment.