Skip to content

Commit

Permalink
refactor: Migrate ErrorBoundary to typescript (apache#27143)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael S. Molina <[email protected]>
  • Loading branch information
EnxDev and michael-s-molina authored Feb 16, 2024
1 parent 8749d9f commit 1776405
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
import React from 'react';
import { render, screen } from 'spec/helpers/testing-library';
import ErrorBoundary from '.';
import ErrorBoundary, { ErrorBoundaryProps } from '.';

const mockedProps = {
const mockedProps: Partial<ErrorBoundaryProps> = {
children: <span>Error children</span>,
onError: () => null,
onError: jest.fn(),
showMessage: false,
};

const Child = () => {
const Child = (): React.ReactElement => {
throw new Error('Thrown error');
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,58 @@
* under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { t } from '@superset-ui/core';
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';

const propTypes = {
children: PropTypes.node.isRequired,
onError: PropTypes.func,
showMessage: PropTypes.bool,
};
const defaultProps = {
onError: () => {},
showMessage: true,
};
export interface ErrorBoundaryProps {
children: React.ReactNode;
onError?: (error: Error, info: React.ErrorInfo) => void;
showMessage?: boolean;
}

interface ErrorBoundaryState {
error: Error | null;
info: React.ErrorInfo | null;
}

export default class ErrorBoundary extends React.Component {
constructor(props) {
export default class ErrorBoundary extends React.Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
static defaultProps: Partial<ErrorBoundaryProps> = {
showMessage: true,
};

constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { error: null, info: null };
}

componentDidCatch(error, info) {
if (this.props.onError) this.props.onError(error, info);
componentDidCatch(error: Error, info: React.ErrorInfo): void {
this.props.onError?.(error, info);
this.setState({ error, info });
}

render() {
const { error, info } = this.state;
if (error) {
const firstLine = error.toString();
const message = (
const messageString = `${t('Unexpected error')}${
firstLine ? `: ${firstLine}` : ''
}`;
const messageElement = (
<span>
<strong>{t('Unexpected error')}</strong>
{firstLine ? `: ${firstLine}` : ''}
</span>
);

if (this.props.showMessage) {
return (
<ErrorMessageWithStackTrace
subtitle={message}
copyText={message}
stackTrace={info ? info.componentStack : null}
subtitle={messageElement}
copyText={messageString}
stackTrace={info?.componentStack}
/>
);
}
Expand All @@ -66,5 +77,3 @@ export default class ErrorBoundary extends React.Component {
return this.props.children;
}
}
ErrorBoundary.propTypes = propTypes;
ErrorBoundary.defaultProps = defaultProps;

0 comments on commit 1776405

Please sign in to comment.