Skip to content

Commit

Permalink
Merge pull request #6508 from marmelab/fix-error-coundary-type
Browse files Browse the repository at this point in the history
[TypeScript] Fix Error prop types
  • Loading branch information
fzaninotto authored Aug 13, 2021
2 parents 8219644 + 92ff269 commit 2693435
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/layout/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ const Error = (props: ErrorProps): JSX.Element => {
Error.propTypes = {
classes: PropTypes.object,
className: PropTypes.string,
error: PropTypes.oneOfType([PropTypes.object, PropTypes.string]).isRequired,
error: PropTypes.object.isRequired,
errorInfo: PropTypes.object,
title: TitlePropType,
};

export interface ErrorProps extends HtmlHTMLAttributes<HTMLDivElement> {
classes?: ClassesOverride<typeof useStyles>;
className?: string;
error: any;
error: Error;
errorInfo?: ErrorInfo;
title?: string;
}
Expand Down
34 changes: 18 additions & 16 deletions packages/ra-ui-materialui/src/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LayoutWithoutTheme extends Component<
LayoutWithoutThemeProps,
LayoutState
> {
state = { hasError: false, errorMessage: null, errorInfo: null };
state: LayoutState = { hasError: false, error: null, errorInfo: null };

constructor(props) {
super(props);
Expand All @@ -95,8 +95,8 @@ class LayoutWithoutTheme extends Component<
});
}

componentDidCatch(errorMessage, errorInfo) {
this.setState({ hasError: true, errorMessage, errorInfo });
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.setState({ hasError: true, error, errorInfo });
}

render() {
Expand All @@ -105,7 +105,7 @@ class LayoutWithoutTheme extends Component<
children,
classes,
className,
error,
error: ErrorComponent,
dashboard,
logout,
menu,
Expand All @@ -120,7 +120,7 @@ class LayoutWithoutTheme extends Component<
staticContext,
...props
} = this.props;
const { hasError, errorMessage, errorInfo } = this.state;
const { hasError, error, errorInfo } = this.state;
return (
<>
<div
Expand All @@ -138,13 +138,15 @@ class LayoutWithoutTheme extends Component<
}),
})}
<div id="main-content" className={classes.content}>
{hasError
? createElement(error, {
error: errorMessage,
errorInfo,
title,
})
: children}
{hasError ? (
<ErrorComponent
error={error}
errorInfo={errorInfo}
title={title}
/>
) : (
children
)}
</div>
</main>
</div>
Expand Down Expand Up @@ -186,8 +188,8 @@ export interface LayoutProps
classes?: any;
className?: string;
error?: ComponentType<{
error?: string;
errorInfo?: React.ErrorInfo;
error?: Error;
errorInfo?: ErrorInfo;
title?: string | ReactElement<any>;
}>;
menu?: ComponentType<MenuProps>;
Expand All @@ -198,8 +200,8 @@ export interface LayoutProps

export interface LayoutState {
hasError: boolean;
errorMessage: string;
errorInfo: ErrorInfo;
error?: Error;
errorInfo?: ErrorInfo;
}

interface LayoutWithoutThemeProps
Expand Down

0 comments on commit 2693435

Please sign in to comment.