1
0
Fork 0
freesewing/sites/shared/components/error/error-boundary.mjs

68 lines
1.7 KiB
JavaScript
Raw Normal View History

import React from 'react'
import { ResetButtons } from './reset-buttons'
import { LogGroup } from 'shared/components/workbench/logs'
import { ErrorView as DefaultErrorView } from './view'
2022-07-14 07:08:41 -05:00
const ErrorView = (props) => {
if (props.children) return props.children
const inspectChildrenProps = {
type: 'error',
logs: [props.error],
units: props.gist?.units,
2022-07-14 07:08:41 -05:00
}
const inspectChildren = <LogGroup {...inspectChildrenProps}></LogGroup>
return (
props.children || (
<DefaultErrorView inspectChildren={inspectChildren}>
2022-07-14 07:08:41 -05:00
<h4>If you think your last action caused this error, you can: </h4>
<ResetButtons undoGist={props.undoGist} resetGist={props.resetGist} />
</DefaultErrorView>
)
)
2022-07-14 07:08:41 -05:00
}
2022-07-11 15:32:51 -05:00
export class ErrorBoundary extends React.Component {
2022-07-11 15:32:51 -05:00
constructor(props) {
super(props)
this.state = { hasError: false }
2022-07-11 15:32:51 -05:00
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true, error }
2022-07-11 15:32:51 -05:00
}
componentDidCatch(error, errorInfo) {
// You can also log the error to an error reporting service
console.log(error, errorInfo)
2022-07-11 15:32:51 -05:00
}
componentDidUpdate(prevProps) {
if (this.props.gist !== prevProps.gist) {
this.setState({ hasError: false })
2022-07-11 15:32:51 -05:00
}
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return (
<ErrorView {...this.props} error={this.state.error}>
{this.errorView}
</ErrorView>
)
2022-07-11 15:32:51 -05:00
}
try {
return this.props.children
} catch (e) {
return (
<ErrorView {...this.props} error={e}>
{this.errorView}
</ErrorView>
)
2022-07-11 15:32:51 -05:00
}
}
}