-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77ac0c2
commit 1d50119
Showing
2 changed files
with
78 additions
and
20 deletions.
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
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,55 @@ | ||
import React, { Component, ErrorInfo, ReactNode } from "react"; | ||
import { sendReport } from "../../utils/humbug"; | ||
|
||
interface ErrorBoundaryProps { | ||
children: ReactNode; | ||
} | ||
|
||
interface ErrorBoundaryState { | ||
hasError: boolean; | ||
error: Error | null; | ||
errorInfo: ErrorInfo | null; | ||
} | ||
|
||
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
state: ErrorBoundaryState = { | ||
hasError: false, | ||
error: null, | ||
errorInfo: null, | ||
}; | ||
|
||
static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> { | ||
return { hasError: true, error }; | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
this.setState({ errorInfo }); | ||
console.error("Uncaught error:", error, errorInfo); | ||
const content = JSON.stringify({ | ||
error: error.toString(), | ||
errorInfo: errorInfo.componentStack, | ||
}); | ||
sendReport("React ErrorBoundary Error", content, ["type:error", "error_domain:react"]).catch( | ||
(reportError) => { | ||
console.error("Failed to send error report:", reportError); | ||
}, | ||
); | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<div> | ||
<h2>Oops, there is an error!</h2> | ||
<button type="button" onClick={() => this.setState({ hasError: false })}> | ||
Try again? | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |