Core: Show exception rather than error on react error boundary (#8100)

Core: Show exception rather than error on react error boundary
This commit is contained in:
Michael Shilman 2019-10-04 15:10:58 +08:00 committed by Michael Shilman
parent d08130eae9
commit aeda584a1d
2 changed files with 8 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import { document } from 'global';
import React from 'react';
import ReactDOM from 'react-dom';
import { RenderMainArgs, ShowErrorArgs } from './types';
import { RenderMainArgs } from './types';
const rootEl = document ? document.getElementById('root') : null;
@ -15,7 +15,7 @@ const render = (node: React.ReactElement, el: Element) =>
});
class ErrorBoundary extends React.Component<{
showError: (args: ShowErrorArgs) => void;
showException: (err: Error) => void;
showMain: () => void;
}> {
state = { hasError: false };
@ -32,10 +32,10 @@ class ErrorBoundary extends React.Component<{
}
}
componentDidCatch({ message, stack }: Error) {
const { showError } = this.props;
componentDidCatch(err: Error) {
const { showException } = this.props;
// message partially duplicates stack, strip it
showError({ title: message.split(/\n/)[0], description: stack });
showException(err);
}
render() {
@ -49,11 +49,11 @@ class ErrorBoundary extends React.Component<{
export default async function renderMain({
storyFn: StoryFn,
showMain,
showError,
showException,
forceRender,
}: RenderMainArgs) {
const element = (
<ErrorBoundary showMain={showMain} showError={showError}>
<ErrorBoundary showMain={showMain} showException={showException}>
<StoryFn />
</ErrorBoundary>
);

View File

@ -11,6 +11,7 @@ export interface RenderMainArgs {
selectedStory: string;
showMain: () => void;
showError: (args: ShowErrorArgs) => void;
showException: (err: Error) => void;
forceRender: boolean;
}