@boomerang-io/carbon-addons-boomerang-react
Version:
Carbon Addons for Boomerang apps
40 lines (37 loc) • 1.27 kB
JavaScript
import React, { Component } from 'react';
import { prefix } from '../../internal/settings.js';
import Error from '../ErrorMessage/ErrorMessage.js';
/*
IBM Confidential
694970X, 69497O0
© Copyright IBM Corp. 2022, 2024
*/
/**
* todo: convert this to hooks in the future.
* As of now getDerivedStateFromError not available via hooks
*/
class ErrorBoundary extends Component {
state = {
hasError: false,
error: {},
};
static defaultProps = {
className: `${prefix}--bmrg-error-boundary`,
};
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true, error };
}
componentDidCatch(error, info) {
console.error(error, info); //eslint-disable-line
}
render() {
const { errorComponent: ErrorComponent = Error, className, style, errorProps, ...rest } = this.props;
if (this.state.hasError) {
return (React.createElement("div", { className: className, style: style, ...rest },
React.createElement(ErrorComponent, { ...this.props.errorProps })));
}
return React.createElement(React.Fragment, null, this.props.children);
}
}
export { ErrorBoundary as default };