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

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-07-11 15:32:51 -05:00
import React from 'react';
import ResetButtons from './reset-buttons'
2022-07-14 07:08:41 -05:00
import {EventGroup} from 'shared/components/workbench/events'
import DefaultErrorView from './view';
const ErrorView = (props) => {
if (props.children) return props.children
const inspectChildrenProps = {
type: 'error',
events: [props.error],
units: props.gist?.units
}
const inspectChildren = (<EventGroup {...inspectChildrenProps}></EventGroup>)
return (props.children || (<DefaultErrorView inspectChildren={inspectChildren}>
<h4>If you think your last action caused this error, you can: </h4>
<ResetButtons undoGist={props.undoGist} resetGist={props.resetGist} />
</DefaultErrorView>))
}
2022-07-11 15:32:51 -05:00
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
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);
}
componentDidUpdate(prevProps) {
if (this.props.gist !== prevProps.gist) {
this.setState({hasError: false})
}
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
2022-07-14 07:08:41 -05:00
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) {
2022-07-14 07:08:41 -05:00
return <ErrorView {...this.props} error={e}>{this.errorView}</ErrorView>;
2022-07-11 15:32:51 -05:00
}
}
}
export default ErrorBoundary