Skip to content

Commit 0320100

Browse files
committed
PR feedback
1 parent b019ef9 commit 0320100

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

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

+66
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,71 @@ describe('ES error parser', () => {
4242
},
4343
})
4444
).toStrictEqual(', caused by: "reason2"');
45+
46+
expect(
47+
getEsErrorMessage({
48+
name: '',
49+
message: '',
50+
meta: {
51+
body: {
52+
error: {
53+
caused_by: {
54+
reason: 'reason3',
55+
caused_by: {
56+
reason: 'reason4',
57+
},
58+
},
59+
},
60+
},
61+
},
62+
})
63+
).toStrictEqual(', caused by: "reason3,reason4"');
64+
65+
expect(
66+
getEsErrorMessage({
67+
name: '',
68+
message: '',
69+
meta: {
70+
body: {
71+
error: {
72+
failed_shards: [
73+
{
74+
reason: {
75+
caused_by: {
76+
reason: 'reason4',
77+
},
78+
},
79+
},
80+
],
81+
},
82+
},
83+
},
84+
})
85+
).toStrictEqual(', caused by: "reason4"');
86+
87+
expect(
88+
getEsErrorMessage({
89+
name: '',
90+
message: '',
91+
meta: {
92+
body: {
93+
error: {
94+
failed_shards: [
95+
{
96+
reason: {
97+
caused_by: {
98+
reason: 'reason5',
99+
caused_by: {
100+
reason: 'reason6',
101+
},
102+
},
103+
},
104+
},
105+
],
106+
},
107+
},
108+
},
109+
})
110+
).toStrictEqual(', caused by: "reason5,reason6"');
45111
});
46112
});

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

+6-17
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,12 @@
66
*/
77

88
// import { ResponseError } from '@elastic/elasticsearch/lib/errors';
9-
import { ElasticsearchError } from './';
9+
import { ElasticsearchError, ElasticsearchErrorCausedByObject } from './types';
1010

11-
interface ErrorObject {
12-
caused_by?: {
13-
reason?: string;
14-
caused_by?: {};
15-
};
16-
failed_shards?: Array<{
17-
reason?: {
18-
caused_by?: {
19-
reason?: string;
20-
};
21-
};
22-
}>;
23-
}
24-
25-
const getEsCause = (obj: ErrorObject = {}, causes: string[] = []): string[] => {
11+
const getEsCause = (
12+
obj: ElasticsearchErrorCausedByObject = {},
13+
causes: string[] = []
14+
): string[] => {
2615
const updated = [...causes];
2716

2817
if (obj.caused_by) {
@@ -49,7 +38,7 @@ export const getEsErrorMessage = (error: ElasticsearchError) => {
4938
let message = error?.message;
5039
const apiError = error?.error?.meta?.body?.error ?? error?.meta?.body?.error;
5140
if (apiError) {
52-
message += `, caused by: "${getEsCause(apiError as ErrorObject)}"`;
41+
message += `, caused by: "${getEsCause(apiError as ElasticsearchErrorCausedByObject)}"`;
5342
}
5443
return message;
5544
};

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ export interface ErrorThatHandlesItsOwnResponse extends Error {
1010
sendResponse(res: KibanaResponseFactory): IKibanaResponse;
1111
}
1212

13-
interface ElasticsearchErrorCausedBy {
14-
caused_by?: {
15-
reason?: string;
16-
};
13+
export interface ElasticsearchErrorCausedByObject {
14+
reason?: string;
15+
caused_by?: ElasticsearchErrorCausedByObject;
16+
failed_shards?: Array<{
17+
reason?: {
18+
caused_by?: ElasticsearchErrorCausedByObject;
19+
};
20+
}>;
1721
}
1822

1923
interface ElasticsearchErrorMeta {
2024
body?: {
21-
error?: ElasticsearchErrorCausedBy;
25+
error?: ElasticsearchErrorCausedByObject;
2226
};
2327
}
2428

0 commit comments

Comments
 (0)