From 554fe98ce49089ea0241b58ef44c7b80aee6aa68 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Wed, 30 Dec 2020 09:18:41 -0500 Subject: [PATCH 1/3] Initial work --- .../server/builtin_action_types/webhook.ts | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/actions/server/builtin_action_types/webhook.ts b/x-pack/plugins/actions/server/builtin_action_types/webhook.ts index 3d872d6e7e311..3aa6e5e2db8e3 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/webhook.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/webhook.ts @@ -176,18 +176,21 @@ export async function executor( const { error } = result; if (error.response) { - const { status, statusText, headers: responseHeaders } = error.response; - const message = `[${status}] ${statusText}`; + const { + headers: responseHeaders, + data: { statusCode, error: errorText, message: errorMessage }, + } = error.response; + const message = `[${statusCode}] ${errorText}: ${errorMessage}`; logger.error(`error on ${actionId} webhook event: ${message}`); // The request was made and the server responded with a status code // that falls out of the range of 2xx // special handling for 5xx - if (status >= 500) { + if (statusCode >= 500) { return retryResult(actionId, message); } // special handling for rate limiting - if (status === 429) { + if (statusCode === 429) { return pipe( getRetryAfterIntervalFromHeaders(responseHeaders), map((retry) => retryResultSeconds(actionId, message, retry)), @@ -195,6 +198,10 @@ export async function executor( ); } return errorResultInvalid(actionId, message); + } else if (error.isAxiosError) { + const message = `[${error.code}] ${error.message}`; + logger.error(`error on ${actionId} webhook event: ${message}`); + return errorResultRequestFailed(actionId, message); } logger.error(`error on ${actionId} webhook action: unexpected error`); @@ -222,6 +229,21 @@ function errorResultInvalid( }; } +function errorResultRequestFailed( + actionId: string, + serviceMessage: string +): ActionTypeExecutorResult { + const errMessage = i18n.translate('xpack.actions.builtin.webhook.requestFailedErrorMessage', { + defaultMessage: 'error calling webhook, request failed', + }); + return { + status: 'error', + message: errMessage, + actionId, + serviceMessage, + }; +} + function errorResultUnexpectedError(actionId: string): ActionTypeExecutorResult { const errMessage = i18n.translate('xpack.actions.builtin.webhook.unreachableErrorMessage', { defaultMessage: 'error calling webhook, unexpected error', From 3f5a53af2f428ed0a8cb55d939cea4201a9b1d67 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Wed, 30 Dec 2020 09:57:21 -0500 Subject: [PATCH 2/3] Fix variables to pull from --- .../plugins/actions/server/builtin_action_types/webhook.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/actions/server/builtin_action_types/webhook.ts b/x-pack/plugins/actions/server/builtin_action_types/webhook.ts index 3aa6e5e2db8e3..18836660c1e89 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/webhook.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/webhook.ts @@ -177,10 +177,13 @@ export async function executor( if (error.response) { const { + status: statusCode, + statusText: errorText, headers: responseHeaders, - data: { statusCode, error: errorText, message: errorMessage }, + data: { message: errorMessage }, } = error.response; - const message = `[${statusCode}] ${errorText}: ${errorMessage}`; + const errorMessageAsSuffix = errorMessage ? `: ${errorMessage}` : ''; + const message = `[${statusCode}] ${errorText}${errorMessageAsSuffix}`; logger.error(`error on ${actionId} webhook event: ${message}`); // The request was made and the server responded with a status code // that falls out of the range of 2xx From bbdea966842b00ae88fe2879b79990bcf679bc6c Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Wed, 30 Dec 2020 10:08:13 -0500 Subject: [PATCH 3/3] Rename some variables --- .../actions/server/builtin_action_types/webhook.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/actions/server/builtin_action_types/webhook.ts b/x-pack/plugins/actions/server/builtin_action_types/webhook.ts index 18836660c1e89..089363990643f 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/webhook.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/webhook.ts @@ -177,23 +177,23 @@ export async function executor( if (error.response) { const { - status: statusCode, - statusText: errorText, + status, + statusText, headers: responseHeaders, - data: { message: errorMessage }, + data: { message: responseMessage }, } = error.response; - const errorMessageAsSuffix = errorMessage ? `: ${errorMessage}` : ''; - const message = `[${statusCode}] ${errorText}${errorMessageAsSuffix}`; + const responseMessageAsSuffix = responseMessage ? `: ${responseMessage}` : ''; + const message = `[${status}] ${statusText}${responseMessageAsSuffix}`; logger.error(`error on ${actionId} webhook event: ${message}`); // The request was made and the server responded with a status code // that falls out of the range of 2xx // special handling for 5xx - if (statusCode >= 500) { + if (status >= 500) { return retryResult(actionId, message); } // special handling for rate limiting - if (statusCode === 429) { + if (status === 429) { return pipe( getRetryAfterIntervalFromHeaders(responseHeaders), map((retry) => retryResultSeconds(actionId, message, retry)),