Skip to content

Commit b7d33a7

Browse files
committed
use root error
1 parent 9073897 commit b7d33a7

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/plugins/expressions/common/util/create_error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ExpressionValueError } from '../../common';
2222
type ErrorLike = Partial<Pick<Error, 'name' | 'message' | 'stack'>>;
2323

2424
export const createError = (
25-
err: string | Error | ErrorLike | ExpressionValueError['error']
25+
err: string | Error | (ErrorLike & { original?: Error })
2626
): ExpressionValueError => ({
2727
type: 'error',
2828
error: {

x-pack/plugins/lens/public/editor_frame_service/error_helper.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ import { i18n } from '@kbn/i18n';
88

99
import { ExpressionRenderError } from 'src/plugins/expressions/public';
1010

11+
interface ElasticsearchErrorClause {
12+
type: string;
13+
reason: string;
14+
caused_by?: ElasticsearchErrorClause;
15+
}
16+
1117
interface RequestError extends Error {
12-
body?: { attributes?: { error: { caused_by: { type: string; reason: string } } } };
18+
body?: { attributes?: { error: ElasticsearchErrorClause } };
1319
}
1420

1521
const isRequestError = (e: Error | RequestError): e is RequestError => {
@@ -19,18 +25,25 @@ const isRequestError = (e: Error | RequestError): e is RequestError => {
1925
return false;
2026
};
2127

28+
function getNestedErrorClause({
29+
type,
30+
reason,
31+
caused_by: causedBy,
32+
}: ElasticsearchErrorClause): { type: string; reason: string } {
33+
if (causedBy) {
34+
return getNestedErrorClause(causedBy);
35+
}
36+
return { type, reason };
37+
}
38+
2239
export function getOriginalRequestErrorMessage(error?: ExpressionRenderError | null) {
23-
return (
24-
error &&
25-
'original' in error &&
26-
error.original &&
27-
isRequestError(error.original) &&
28-
i18n.translate('xpack.lens.editorFrame.expressionFailureMessage', {
29-
defaultMessage: 'Request error: {causedByType}, {causedByReason}',
30-
values: {
31-
causedByType: error.original.body?.attributes?.error?.caused_by.type,
32-
causedByReason: error.original.body?.attributes?.error?.caused_by.reason,
33-
},
34-
})
35-
);
40+
if (error && 'original' in error && error.original && isRequestError(error.original)) {
41+
const rootError = getNestedErrorClause(error.original.body!.attributes!.error);
42+
if (rootError.reason && rootError.type) {
43+
return i18n.translate('xpack.lens.editorFrame.expressionFailureMessage', {
44+
defaultMessage: 'Request error: {type}, {reason}',
45+
values: rootError,
46+
});
47+
}
48+
}
3649
}

0 commit comments

Comments
 (0)