Skip to content

Commit

Permalink
use root error
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Oct 12, 2020
1 parent 9073897 commit b7d33a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/plugins/expressions/common/util/create_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ExpressionValueError } from '../../common';
type ErrorLike = Partial<Pick<Error, 'name' | 'message' | 'stack'>>;

export const createError = (
err: string | Error | ErrorLike | ExpressionValueError['error']
err: string | Error | (ErrorLike & { original?: Error })
): ExpressionValueError => ({
type: 'error',
error: {
Expand Down
41 changes: 27 additions & 14 deletions x-pack/plugins/lens/public/editor_frame_service/error_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import { i18n } from '@kbn/i18n';

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

interface ElasticsearchErrorClause {
type: string;
reason: string;
caused_by?: ElasticsearchErrorClause;
}

interface RequestError extends Error {
body?: { attributes?: { error: { caused_by: { type: string; reason: string } } } };
body?: { attributes?: { error: ElasticsearchErrorClause } };
}

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

function getNestedErrorClause({
type,
reason,
caused_by: causedBy,
}: ElasticsearchErrorClause): { type: string; reason: string } {
if (causedBy) {
return getNestedErrorClause(causedBy);
}
return { type, reason };
}

export function getOriginalRequestErrorMessage(error?: ExpressionRenderError | null) {
return (
error &&
'original' in error &&
error.original &&
isRequestError(error.original) &&
i18n.translate('xpack.lens.editorFrame.expressionFailureMessage', {
defaultMessage: 'Request error: {causedByType}, {causedByReason}',
values: {
causedByType: error.original.body?.attributes?.error?.caused_by.type,
causedByReason: error.original.body?.attributes?.error?.caused_by.reason,
},
})
);
if (error && 'original' in error && error.original && isRequestError(error.original)) {
const rootError = getNestedErrorClause(error.original.body!.attributes!.error);
if (rootError.reason && rootError.type) {
return i18n.translate('xpack.lens.editorFrame.expressionFailureMessage', {
defaultMessage: 'Request error: {type}, {reason}',
values: rootError,
});
}
}
}

0 comments on commit b7d33a7

Please sign in to comment.