Skip to content

Commit 1c25705

Browse files
committed
Fix tests
1 parent c86091c commit 1c25705

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

x-pack/plugins/alerting/server/lib/errors/es_error_parser.test.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,42 @@
55
* 2.0.
66
*/
77

8-
import { getEsCause } from './es_error_parser';
8+
import { getEsErrorMessage } from './es_error_parser';
99

1010
describe('ES error parser', () => {
1111
test('should return all the cause of the error', () => {
1212
expect(
13-
getEsCause({
14-
caused_by: {
15-
reason: 'reason1',
16-
},
17-
})
18-
).toStrictEqual(['reason1']);
19-
20-
expect(
21-
getEsCause({
22-
caused_by: {
23-
reason: 'reason1',
24-
caused_by: {
25-
reason: 'reason2',
13+
getEsErrorMessage({
14+
name: '',
15+
message: '',
16+
error: {
17+
meta: {
18+
body: {
19+
error: {
20+
caused_by: {
21+
reason: 'reason1',
22+
},
23+
},
24+
},
2625
},
2726
},
2827
})
29-
).toStrictEqual(['reason1', 'reason2']);
28+
).toStrictEqual(', caused by: "reason1"');
3029

3130
expect(
32-
getEsCause({
33-
failed_shards: [
34-
{
35-
reason: {
31+
getEsErrorMessage({
32+
name: '',
33+
message: '',
34+
meta: {
35+
body: {
36+
error: {
3637
caused_by: {
37-
reason: 'reason3',
38+
reason: 'reason2',
3839
},
3940
},
4041
},
41-
],
42+
},
4243
})
43-
).toStrictEqual(['reason3']);
44+
).toStrictEqual(', caused by: "reason2"');
4445
});
4546
});

x-pack/plugins/alerting/server/lib/errors/es_error_parser.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ const getEsCause = (obj: ErrorObject = {}, causes: string[] = []): string[] => {
4545
return updated.filter(Boolean);
4646
};
4747

48-
export const getEsErrorMessage = (error: Error) => {
49-
let message = error.message;
50-
const apiError =
51-
(error as ElasticsearchError)?.error?.meta?.body?.error ??
52-
(error as ElasticsearchError)?.meta?.body?.error;
48+
export const getEsErrorMessage = (error: ElasticsearchError) => {
49+
let message = error?.message;
50+
const apiError = error?.error?.meta?.body?.error ?? error?.meta?.body?.error;
5351
if (apiError) {
5452
message += `, caused by: "${getEsCause(apiError as ErrorObject)}"`;
5553
}

x-pack/plugins/alerting/server/lib/errors/types.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
* 2.0; you may not use this file except in compliance with the Elastic License
55
* 2.0.
66
*/
7-
import { ApiError } from '@elastic/elasticsearch';
87
import { KibanaResponseFactory, IKibanaResponse } from '../../../../../../src/core/server';
98

109
export interface ErrorThatHandlesItsOwnResponse extends Error {
1110
sendResponse(res: KibanaResponseFactory): IKibanaResponse;
1211
}
1312

13+
interface ElasticsearchErrorCausedBy {
14+
caused_by?: {
15+
reason?: string;
16+
};
17+
}
18+
1419
interface ElasticsearchErrorMeta {
1520
body?: {
16-
error?: ApiError;
21+
error?: ElasticsearchErrorCausedBy;
1722
};
1823
}
1924

0 commit comments

Comments
 (0)