react-error-boundaries
Version:
React HOC for error boundaries.
130 lines (107 loc) • 5.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var __ErrorBoundary = void 0;
if (process.env.NODE_ENV === "development" || process.env.ERROR_ENV === "development") {
var _require = require("./WithErrorHandler"),
ErrorBoundary = _require.ErrorBoundary,
FallbackView = _require.FallbackView;
var withErrorHandler = curry(function (FallbackComponent, Component) {
var WithErrorHandler = function WithErrorHandler(props) {
var onError = props.onError;
return _react2.default.createElement(
ErrorBoundary,
{ FallbackComponent: FallbackComponent, onError: onError },
_react2.default.createElement(Component, props)
);
};
WithErrorHandler.displayName = "WithErrorHandler(" + (Component.displayName || Component.name || "Component") + ")";
return WithErrorHandler;
});
__ErrorBoundary = ErrorBoundary;
exports.ErrorBoundary = ErrorBoundary;
exports.FallbackView = FallbackView;
exports.withErrorHandler = withErrorHandler;
exports.errorHandlerDecorator = withErrorHandler(FallbackView);
} else {
// production or other env (not development)
// NOOP ErrorBoundary
var _ErrorBoundary = function (_React$Component) {
_inherits(_ErrorBoundary, _React$Component);
function _ErrorBoundary() {
_classCallCheck(this, _ErrorBoundary);
return _possibleConstructorReturn(this, (_ErrorBoundary.__proto__ || Object.getPrototypeOf(_ErrorBoundary)).apply(this, arguments));
}
_createClass(_ErrorBoundary, [{
key: "componentDidCatch",
value: function componentDidCatch(error, info) {
var _props2 = this.props,
onError = _props2.onError,
_props = _objectWithoutProperties(_props2, ["onError"]);
if (typeof onError === "function") {
try {
onError.call(this, error, info, _props);
} catch (e) {}
}
}
}, {
key: "render",
value: function render() {
return this.props.children;
}
}]);
return _ErrorBoundary;
}(_react2.default.Component);
// NOOP HOC
var _withErrorHandler = curry(function (FallbackComponent, Component) {
var WithErrorHandler = function WithErrorHandler(props) {
var onError = props.onError;
return _react2.default.createElement(
_ErrorBoundary,
{ FallbackComponent: FallbackComponent, onError: onError },
_react2.default.createElement(Component, props)
);
};
return WithErrorHandler;
});
__ErrorBoundary = _ErrorBoundary;
exports.ErrorBoundary = _ErrorBoundary;
exports.withErrorHandler = _withErrorHandler;
exports.errorHandlerDecorator = _withErrorHandler(void 0);
}
function curry(fn) {
if (typeof fn !== "function") {
throw Error("curry only receive function params!");
}
var _len = fn.length,
_args = [];
function _curry() {
var args = [].concat(_args);
if (arguments.length >= _len) {
_args = [];
} else if (arguments.length + _args.length > _len) {
_args = [];
}
_args = _args.concat([].slice.call(arguments));
if (_args.length === _len) {
var rst = fn.apply(null, _args);
_args = args;
return rst;
}
return _curry;
}
_curry.toString = function () {
return fn.toString();
};
return _curry;
}
exports.default = __ErrorBoundary;