-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Define a custom `toJSON` method on errors from the ES client to have better control of the format of the output of their JSON serialization (cherry picked from commit c43e699)
- Loading branch information
1 parent
e5046ca
commit 44c76a7
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/core/elasticsearch/core-elasticsearch-client-server-internal/src/patch_client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { errors } from '@elastic/elasticsearch'; | ||
|
||
export const patchElasticsearchClient = () => { | ||
const baseErrorPrototype = errors.ElasticsearchClientError.prototype; | ||
// @ts-expect-error | ||
baseErrorPrototype.toJSON = function () { | ||
return { | ||
name: this.name, | ||
message: this.message, | ||
}; | ||
}; | ||
}; |
70 changes: 70 additions & 0 deletions
70
src/core/server/integration_tests/elasticsearch/errors.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { | ||
createTestServers, | ||
type TestElasticsearchUtils, | ||
type TestKibanaUtils, | ||
} from '@kbn/core-test-helpers-kbn-server'; | ||
|
||
describe('elasticsearch clients errors', () => { | ||
let esServer: TestElasticsearchUtils; | ||
let kibanaServer: TestKibanaUtils; | ||
|
||
beforeAll(async () => { | ||
const { startES, startKibana } = createTestServers({ | ||
adjustTimeout: jest.setTimeout, | ||
}); | ||
|
||
esServer = await startES(); | ||
kibanaServer = await startKibana(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await kibanaServer.stop(); | ||
await esServer.stop(); | ||
}); | ||
|
||
it('has the proper JSON 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(JSON.stringify(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]\\"}"` | ||
); | ||
} | ||
}); | ||
|
||
it('has the proper string 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(String(e)).toMatchInlineSnapshot(` | ||
"ResponseError: parsing_exception | ||
Caused by: | ||
named_object_not_found_exception: [1:30] unknown field [someInvalidQuery] | ||
Root causes: | ||
parsing_exception: unknown query [someInvalidQuery]" | ||
`); | ||
} | ||
}); | ||
}); |