@rollbar/react
Version:
Effortlessly track and debug errors in your React applications with Rollbar. This package includes advanced error tracking features and a set of React-specific enhancements to help you identify and fix issues more quickly.
92 lines (89 loc) • 3.33 kB
JavaScript
"use client";
import { defineProperty as _defineProperty, inherits as _inherits, createClass as _createClass, classCallCheck as _classCallCheck, callSuper as _callSuper, assertThisInitialized as _assertThisInitialized, objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import invariant from './external/tiny-invariant/dist/esm/tiny-invariant.js';
import { LEVEL_ERROR } from './constant.js';
import { Context, getRollbarFromContext } from './Provider.js';
import { isValidLevel, value } from './utils.js';
var INITIAL_ERROR_STATE = {
hasError: false,
error: null
};
var ErrorBoundary = /*#__PURE__*/function (_Component) {
_inherits(ErrorBoundary, _Component);
function ErrorBoundary(props) {
var _this;
_classCallCheck(this, ErrorBoundary);
_this = _callSuper(this, ErrorBoundary, [props]);
_defineProperty(_assertThisInitialized(_this), "resetError", function () {
_this.setState(INITIAL_ERROR_STATE);
});
invariant(isValidLevel(props.level), "".concat(props.level, " is not a valid level setting for Rollbar"));
_this.state = _objectSpread2({}, INITIAL_ERROR_STATE);
return _this;
}
_createClass(ErrorBoundary, [{
key: "componentDidCatch",
value: function componentDidCatch(error, info) {
var _this$props = this.props,
errorMessage = _this$props.errorMessage,
extra = _this$props.extra,
targetLevel = _this$props.level,
callback = _this$props.callback;
var custom = value(extra, {}, error, info);
var data = _objectSpread2(_objectSpread2({}, info), custom);
var level = value(targetLevel, LEVEL_ERROR, error, info);
var rollbar = getRollbarFromContext(this.context);
if (!errorMessage) {
rollbar[level](error, data, callback);
} else {
var logMessage = value(errorMessage, '', error, info);
rollbar[level](logMessage, error, data, callback);
}
}
}, {
key: "render",
value: function render() {
var _this$state = this.state,
hasError = _this$state.hasError,
error = _this$state.error;
var _this$props2 = this.props,
FallbackUI = _this$props2.fallbackUI,
children = _this$props2.children;
if (!hasError) {
return children;
}
if (!FallbackUI) {
return null;
}
return /*#__PURE__*/React.createElement(FallbackUI, {
error: error,
resetError: this.resetError
});
}
}], [{
key: "getDerivedStateFromError",
value: function getDerivedStateFromError(error) {
return {
hasError: true,
error: error
};
}
}]);
return ErrorBoundary;
}(Component);
_defineProperty(ErrorBoundary, "contextType", Context);
_defineProperty(ErrorBoundary, "propTypes", {
fallbackUI: PropTypes.elementType,
errorMessage: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
extra: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
level: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
callback: PropTypes.func,
children: PropTypes.node
});
_defineProperty(ErrorBoundary, "defaultProps", {
level: LEVEL_ERROR
});
export { ErrorBoundary };
//# sourceMappingURL=ErrorBoundary.js.map