This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
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
90eba54
commit 9cf39f6
Showing
1 changed file
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {createElement, Component} from "react" | ||
import {node} from "prop-types" | ||
|
||
import getName from "lib/helper/component/getName" | ||
|
||
function createErrorBoundary(Target) { | ||
class Error extends Component { | ||
static displayName = `Error(${getName(Target)})` | ||
|
||
static propTypes = { | ||
children: node.isRequired | ||
} | ||
|
||
static getDerivedStateFromError = error => ({error}) | ||
|
||
componentDidCatch(error, info) { | ||
console.error(error) | ||
|
||
if (process.env.NODE_ENV !== "production" && info) { | ||
console.error(info) | ||
} | ||
} | ||
|
||
render() { | ||
const {error} = this.state | ||
|
||
if (!error) { | ||
return this.props.children | ||
} | ||
|
||
// Throw suspenders further | ||
if (error instanceof Promise) { | ||
throw error | ||
} | ||
|
||
return createElement(Target, {error}) | ||
} | ||
} | ||
|
||
return Error | ||
} | ||
|
||
export default createErrorBoundary |