Skip to content

Commit

Permalink
[Transform] extract error message from the root cause
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Dec 9, 2020
1 parent 9b06f7e commit 4099aa7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion x-pack/plugins/transform/server/routes/api/error_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ function extractCausedByChain(
* @return Object Boom error response
*/
export function wrapEsError(err: any, statusCodeToMessageMap: Record<string, any> = {}) {
const { statusCode, body } = err;
const {
meta: { body, statusCode },
} = err;

const {
error: {
Expand All @@ -130,6 +132,12 @@ export function wrapEsError(err: any, statusCodeToMessageMap: Record<string, any

// @ts-expect-error cause is not defined on payload type
boomError.output.payload.cause = causedByChain.length ? causedByChain : defaultCause;

// Set error message based on the root cause
if (root_cause?.[0]) {
boomError.message = extractErrorMessage(root_cause[0]);
}

return boomError;
}

Expand All @@ -138,3 +146,28 @@ export function wrapEsError(err: any, statusCodeToMessageMap: Record<string, any
const message = statusCodeToMessageMap[statusCode];
return new Boom(message, { statusCode });
}

interface EsError {
type: string;
reason: string;
line?: number;
col?: number;
}

/**
* Returns an error message based on the root cause
*/
function extractErrorMessage({ type, reason, line, col }: EsError): string {
let message = `[${type}] ${reason}`;

if (line !== undefined && col !== undefined) {
message +=
', ' +
i18n.translate('xpack.transform.models.transformService.withColAndLineMessage', {
defaultMessage: 'with line={line} & col={col}',
values: { line, col },
});
}

return message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default ({ getService }: FtrProviderContext) => {
.expect(400);

expect(body.message).to.eql(
'[parsing_exception] Unknown aggregation type [value_countt] did you mean [value_count]?, with { line=1 & col=43 }'
'[parsing_exception] Unknown aggregation type [value_countt] did you mean [value_count]?, with line=1 & col=43'
);
});

Expand Down

0 comments on commit 4099aa7

Please sign in to comment.