From efbd53259aba4ae13124638ab0dd7e12599cd9ec Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Wed, 1 Jul 2020 09:25:19 +0200 Subject: [PATCH 1/4] Add caused_by.type and caused_by.reason to error toast modal --- .../notifications/toasts/error_toast.tsx | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/core/public/notifications/toasts/error_toast.tsx b/src/core/public/notifications/toasts/error_toast.tsx index 6b53719839b0f..0e05feefa856d 100644 --- a/src/core/public/notifications/toasts/error_toast.tsx +++ b/src/core/public/notifications/toasts/error_toast.tsx @@ -31,18 +31,21 @@ import { } from '@elastic/eui'; import { EuiSpacer } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; - -import { OverlayStart } from '../../overlays'; +import { OverlayStart } from 'kibana/public'; import { I18nStart } from '../../i18n'; interface ErrorToastProps { title: string; - error: Error; + error: RequestError; toastMessage: string; openModal: OverlayStart['openModal']; i18nContext: () => I18nStart['Context']; } +interface RequestError extends Error { + body?: { attributes?: { error: { caused_by: { type: string; reason: string } } } }; +} + /** * This should instead be replaced by the overlay service once it's available. * This does not use React portals so that if the parent toast times out, this modal @@ -56,6 +59,17 @@ function showErrorDialog({ i18nContext, }: Pick) { const I18nContext = i18nContext(); + let text = ''; + + if (error.body?.attributes?.error?.caused_by) { + text += `${error.body.attributes.error.caused_by.type}\n`; + text += `${error.body.attributes.error.caused_by.reason}\n\n`; + } + + if (error.stack) { + text += error.stack; + } + const modal = openModal( mount( @@ -65,11 +79,11 @@ function showErrorDialog({ - {error.stack && ( + {text && ( - {error.stack} + {text} )} From 9f037257274f61c4316b0d13613154639574bc8a Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Thu, 2 Jul 2020 14:54:21 +0200 Subject: [PATCH 2/4] Add isRequestError function --- src/core/public/notifications/toasts/error_toast.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/public/notifications/toasts/error_toast.tsx b/src/core/public/notifications/toasts/error_toast.tsx index 0e05feefa856d..0c95982f523f1 100644 --- a/src/core/public/notifications/toasts/error_toast.tsx +++ b/src/core/public/notifications/toasts/error_toast.tsx @@ -36,7 +36,7 @@ import { I18nStart } from '../../i18n'; interface ErrorToastProps { title: string; - error: RequestError; + error: Error; toastMessage: string; openModal: OverlayStart['openModal']; i18nContext: () => I18nStart['Context']; @@ -46,6 +46,9 @@ interface RequestError extends Error { body?: { attributes?: { error: { caused_by: { type: string; reason: string } } } }; } +const isRequestError = (e: Error): e is RequestError => + e.body?.attributes?.error?.caused_by !== undefined; + /** * This should instead be replaced by the overlay service once it's available. * This does not use React portals so that if the parent toast times out, this modal @@ -61,7 +64,7 @@ function showErrorDialog({ const I18nContext = i18nContext(); let text = ''; - if (error.body?.attributes?.error?.caused_by) { + if (isRequestError(error)) { text += `${error.body.attributes.error.caused_by.type}\n`; text += `${error.body.attributes.error.caused_by.reason}\n\n`; } From dd6fc5d655bf61e1e3714d8810b1a4fad25ce9a6 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 13 Jul 2020 13:08:28 +0200 Subject: [PATCH 3/4] Fix types --- src/core/public/notifications/toasts/error_toast.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/public/notifications/toasts/error_toast.tsx b/src/core/public/notifications/toasts/error_toast.tsx index 0c95982f523f1..d35146d2675f1 100644 --- a/src/core/public/notifications/toasts/error_toast.tsx +++ b/src/core/public/notifications/toasts/error_toast.tsx @@ -46,7 +46,7 @@ interface RequestError extends Error { body?: { attributes?: { error: { caused_by: { type: string; reason: string } } } }; } -const isRequestError = (e: Error): e is RequestError => +const isRequestError = (e: any): e is RequestError => e.body?.attributes?.error?.caused_by !== undefined; /** @@ -65,8 +65,8 @@ function showErrorDialog({ let text = ''; if (isRequestError(error)) { - text += `${error.body.attributes.error.caused_by.type}\n`; - text += `${error.body.attributes.error.caused_by.reason}\n\n`; + text += `${error?.body?.attributes?.error?.caused_by.type}\n`; + text += `${error?.body?.attributes?.error?.caused_by.reason}\n\n`; } if (error.stack) { From 0febd1ed797864169785224c07f2fcffd9f25ee8 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 13 Jul 2020 16:03:59 +0200 Subject: [PATCH 4/4] Improve types --- src/core/public/notifications/toasts/error_toast.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/public/notifications/toasts/error_toast.tsx b/src/core/public/notifications/toasts/error_toast.tsx index d35146d2675f1..df8214ce771af 100644 --- a/src/core/public/notifications/toasts/error_toast.tsx +++ b/src/core/public/notifications/toasts/error_toast.tsx @@ -46,8 +46,12 @@ interface RequestError extends Error { body?: { attributes?: { error: { caused_by: { type: string; reason: string } } } }; } -const isRequestError = (e: any): e is RequestError => - e.body?.attributes?.error?.caused_by !== undefined; +const isRequestError = (e: Error | RequestError): e is RequestError => { + if ('body' in e) { + return e.body?.attributes?.error?.caused_by !== undefined; + } + return false; +}; /** * This should instead be replaced by the overlay service once it's available.