Skip to content

Commit

Permalink
Use ErrorBoundary to catch both render-time and mount-time errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaloptimist committed Sep 3, 2024
1 parent 60b7472 commit f9190a2
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 187 deletions.
45 changes: 45 additions & 0 deletions client/Components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as React from "react";

interface ErrorBoundaryProps {
children: React.ReactNode;
renderError: (error: Error, errorInfo: React.ErrorInfo) => React.ReactNode;
}

interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
errorInfo: React.ErrorInfo | null;
}

class ErrorBoundary extends React.Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
state: ErrorBoundaryState = {
hasError: false,
error: null,
errorInfo: null
};

constructor(props: ErrorBoundaryProps) {
super(props);
}

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

render() {
if (this.state.hasError) {
return this.props.renderError(this.state.error, this.state.errorInfo);
}

return this.props.children;
}
}

export default ErrorBoundary;
Loading

0 comments on commit f9190a2

Please sign in to comment.