Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
143 lines
4.3 KiB
Plaintext
143 lines
4.3 KiB
Plaintext
import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
|
|
import * as React from 'react';
|
|
|
|
var changedArray = function changedArray(a, b) {
|
|
if (a === void 0) {
|
|
a = [];
|
|
}
|
|
|
|
if (b === void 0) {
|
|
b = [];
|
|
}
|
|
|
|
return a.length !== b.length || a.some(function (item, index) {
|
|
return !Object.is(item, b[index]);
|
|
});
|
|
};
|
|
|
|
var initialState = {
|
|
error: null
|
|
};
|
|
|
|
var ErrorBoundary = /*#__PURE__*/function (_React$Component) {
|
|
_inheritsLoose(ErrorBoundary, _React$Component);
|
|
|
|
function ErrorBoundary() {
|
|
var _this;
|
|
|
|
for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
_args[_key] = arguments[_key];
|
|
}
|
|
|
|
_this = _React$Component.call.apply(_React$Component, [this].concat(_args)) || this;
|
|
_this.state = initialState;
|
|
|
|
_this.resetErrorBoundary = function () {
|
|
var _this$props;
|
|
|
|
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
args[_key2] = arguments[_key2];
|
|
}
|
|
|
|
_this.props.onReset == null ? void 0 : (_this$props = _this.props).onReset.apply(_this$props, args);
|
|
|
|
_this.reset();
|
|
};
|
|
|
|
return _this;
|
|
}
|
|
|
|
ErrorBoundary.getDerivedStateFromError = function getDerivedStateFromError(error) {
|
|
return {
|
|
error: error
|
|
};
|
|
};
|
|
|
|
var _proto = ErrorBoundary.prototype;
|
|
|
|
_proto.reset = function reset() {
|
|
this.setState(initialState);
|
|
};
|
|
|
|
_proto.componentDidCatch = function componentDidCatch(error, info) {
|
|
var _this$props$onError, _this$props2;
|
|
|
|
(_this$props$onError = (_this$props2 = this.props).onError) == null ? void 0 : _this$props$onError.call(_this$props2, error, info);
|
|
};
|
|
|
|
_proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
|
|
var error = this.state.error;
|
|
var resetKeys = this.props.resetKeys; // There's an edge case where if the thing that triggered the error
|
|
// happens to *also* be in the resetKeys array, we'd end up resetting
|
|
// the error boundary immediately. This would likely trigger a second
|
|
// error to be thrown.
|
|
// So we make sure that we don't check the resetKeys on the first call
|
|
// of cDU after the error is set
|
|
|
|
if (error !== null && prevState.error !== null && changedArray(prevProps.resetKeys, resetKeys)) {
|
|
var _this$props$onResetKe, _this$props3;
|
|
|
|
(_this$props$onResetKe = (_this$props3 = this.props).onResetKeysChange) == null ? void 0 : _this$props$onResetKe.call(_this$props3, prevProps.resetKeys, resetKeys);
|
|
this.reset();
|
|
}
|
|
};
|
|
|
|
_proto.render = function render() {
|
|
var error = this.state.error;
|
|
var _this$props4 = this.props,
|
|
fallbackRender = _this$props4.fallbackRender,
|
|
FallbackComponent = _this$props4.FallbackComponent,
|
|
fallback = _this$props4.fallback;
|
|
|
|
if (error !== null) {
|
|
var _props = {
|
|
error: error,
|
|
resetErrorBoundary: this.resetErrorBoundary
|
|
};
|
|
|
|
if ( /*#__PURE__*/React.isValidElement(fallback)) {
|
|
return fallback;
|
|
} else if (typeof fallbackRender === 'function') {
|
|
return fallbackRender(_props);
|
|
} else if (FallbackComponent) {
|
|
return /*#__PURE__*/React.createElement(FallbackComponent, _props);
|
|
} else {
|
|
throw new Error('react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop');
|
|
}
|
|
}
|
|
|
|
return this.props.children;
|
|
};
|
|
|
|
return ErrorBoundary;
|
|
}(React.Component);
|
|
|
|
function withErrorBoundary(Component, errorBoundaryProps) {
|
|
var Wrapped = function Wrapped(props) {
|
|
return /*#__PURE__*/React.createElement(ErrorBoundary, errorBoundaryProps, /*#__PURE__*/React.createElement(Component, props));
|
|
}; // Format for display in DevTools
|
|
|
|
|
|
var name = Component.displayName || Component.name || 'Unknown';
|
|
Wrapped.displayName = "withErrorBoundary(" + name + ")";
|
|
return Wrapped;
|
|
}
|
|
|
|
function useErrorHandler(givenError) {
|
|
var _React$useState = React.useState(null),
|
|
error = _React$useState[0],
|
|
setError = _React$useState[1];
|
|
|
|
if (givenError != null) throw givenError;
|
|
if (error != null) throw error;
|
|
return setError;
|
|
}
|
|
/*
|
|
eslint
|
|
@typescript-eslint/sort-type-union-intersection-members: "off",
|
|
@typescript-eslint/no-throw-literal: "off",
|
|
@typescript-eslint/prefer-nullish-coalescing: "off"
|
|
*/
|
|
|
|
export { ErrorBoundary, useErrorHandler, withErrorBoundary };
|