Skip to content

Commit

Permalink
[Discover] Add caused_by.type and caused_by.reason to error toast mod…
Browse files Browse the repository at this point in the history
…al (#70404)
  • Loading branch information
kertal authored Jul 14, 2020
1 parent b7a6cff commit 24d29a3
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/core/public/notifications/toasts/error_toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ 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 {
Expand All @@ -43,6 +42,17 @@ interface ErrorToastProps {
i18nContext: () => I18nStart['Context'];
}

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

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.
* This does not use React portals so that if the parent toast times out, this modal
Expand All @@ -56,6 +66,17 @@ function showErrorDialog({
i18nContext,
}: Pick<ErrorToastProps, 'error' | 'title' | 'openModal' | 'i18nContext'>) {
const I18nContext = i18nContext();
let text = '';

if (isRequestError(error)) {
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(
<React.Fragment>
Expand All @@ -65,11 +86,11 @@ function showErrorDialog({
</EuiModalHeader>
<EuiModalBody>
<EuiCallOut size="s" color="danger" iconType="alert" title={error.message} />
{error.stack && (
{text && (
<React.Fragment>
<EuiSpacer size="s" />
<EuiCodeBlock isCopyable={true} paddingSize="s">
{error.stack}
{text}
</EuiCodeBlock>
</React.Fragment>
)}
Expand Down

0 comments on commit 24d29a3

Please sign in to comment.