Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Add caused_by.type and caused_by.reason to error toast modal #70404

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