2017-10-13 13:01:27 +00:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { showError } from './modals/index';
|
2017-10-13 13:01:27 +00:00
|
|
|
import Mailto from './base/mailto';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: React.Node,
|
|
|
|
errorClassName?: string,
|
|
|
|
showAlert?: boolean,
|
2018-12-12 17:36:11 +00:00
|
|
|
replaceWith?: React.Node,
|
2017-10-13 13:01:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
error: Error | null,
|
2018-12-12 17:36:11 +00:00
|
|
|
info: { componentStack: string } | null,
|
2017-10-13 13:01:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SingleErrorBoundary extends React.PureComponent<Props, State> {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props: Props) {
|
2017-10-13 13:01:27 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
error: null,
|
2018-12-12 17:36:11 +00:00
|
|
|
info: null,
|
2017-10-13 13:01:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentDidCatch(error: Error, info: { componentStack: string }) {
|
|
|
|
const { children } = this.props;
|
2018-10-17 16:42:33 +00:00
|
|
|
const firstChild = Array.isArray(children) && children.length === 1 ? children[0] : children;
|
2017-10-13 13:01:27 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
this.setState({ error, info });
|
2017-10-13 13:01:27 +00:00
|
|
|
|
|
|
|
let componentName = 'component';
|
|
|
|
try {
|
|
|
|
componentName = (firstChild: any).type.name;
|
|
|
|
} catch (err) {
|
|
|
|
// It's okay
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.showAlert) {
|
|
|
|
try {
|
|
|
|
showError({
|
|
|
|
error,
|
|
|
|
title: 'Application Error',
|
|
|
|
message: (
|
|
|
|
<p>
|
2018-10-17 16:42:33 +00:00
|
|
|
Failed to render {componentName}. Please send the following error to{' '}
|
|
|
|
<Mailto email="support@insomnia.rest" subject="Error Report" body={error.stack} />.
|
2017-10-13 13:01:27 +00:00
|
|
|
</p>
|
2018-12-12 17:36:11 +00:00
|
|
|
),
|
2017-10-13 13:01:27 +00:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
// UI is so broken that we can't even show an alert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
|
|
|
const { error, info } = this.state;
|
|
|
|
const { errorClassName, children } = this.props;
|
2017-10-13 13:01:27 +00:00
|
|
|
|
|
|
|
if (error && info) {
|
2018-10-17 16:42:33 +00:00
|
|
|
return <div className={errorClassName || null}>Render Failure: {error.message}</div>;
|
2017-10-13 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ErrorBoundary extends React.PureComponent<Props> {
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
|
|
|
const { children, ...extraProps } = this.props;
|
2017-10-13 13:01:27 +00:00
|
|
|
|
|
|
|
if (!children) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap multiple children into single children for better error isolation
|
|
|
|
const childArray = Array.isArray(children) ? children : [children];
|
|
|
|
return childArray.map((child, i) => (
|
|
|
|
<SingleErrorBoundary key={i} {...extraProps}>
|
|
|
|
{child}
|
|
|
|
</SingleErrorBoundary>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ErrorBoundary;
|