-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from elite-se/feature/error-boundary
Feature/error boundary
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/frontend/src/components/general/ErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Properties of Error Boundary | ||
*/ | ||
export interface ErrorBoundaryProps { | ||
/** | ||
* Callback used to render ui for the error once it occured | ||
*/ | ||
readonly errorRenderer?: (error: Error) => React.ReactNode; | ||
} | ||
|
||
interface State { | ||
readonly error?: Error; | ||
} | ||
|
||
function renderError(error: Error): React.ReactNode { | ||
return error.message; | ||
} | ||
|
||
/** | ||
* ErrorBoundary component catches all errors that occur somewhere bellow in the DOM | ||
* tree and displays an error message instead of taking the entire page down. | ||
* | ||
* Use by wrapping it around failable components in JSX | ||
* or use the withErrorBoundary(...) to wrap component components | ||
*/ | ||
export class ErrorBoundary extends React.Component<ErrorBoundaryProps, State> { | ||
constructor(props: ErrorBoundaryProps) { | ||
super(props); | ||
this.state = {}; | ||
} | ||
|
||
static getDerivedStateFromError(error: Error) { | ||
return { error }; | ||
} | ||
|
||
componentDidCatch(error: Error, info: React.ErrorInfo) { | ||
console.error('Caught error:', error, 'with info:', info); | ||
console.trace(); | ||
} | ||
|
||
render() { | ||
const { error } = this.state; | ||
if (error) { | ||
return renderError(error); | ||
} | ||
return <>{this.props.children}</>; | ||
} | ||
} | ||
|
||
/** | ||
* ErrorBoundary component catches all errors that occur somewhere bellow in the DOM | ||
* tree and displays an error message instead of taking the entire page down. | ||
* | ||
* Use by wrapping it around failable components in JSX, i.e., <ErrorBoundary>...</ErrorBoundary> | ||
* or using this component wrapper | ||
* | ||
* @param WrappedComponent component to wrap | ||
* @param errorRenderer callback to render error ui | ||
*/ | ||
export function withErrorBoundary<TProps>( | ||
WrappedComponent: React.ComponentType<TProps>, | ||
errorRenderer: (error: Error) => React.ReactNode = renderError, | ||
): React.ComponentType<TProps> { | ||
return (props: TProps) => ( | ||
<ErrorBoundary errorRenderer={errorRenderer}> | ||
<WrappedComponent {...props} /> | ||
</ErrorBoundary> | ||
); | ||
} |