@atlaskit/renderer
Version:
Renderer component
130 lines (129 loc) • 5.89 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ErrorBoundary = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _analytics = require("@atlaskit/editor-common/analytics");
var _types = require("@atlaskit/analytics-listeners/types");
var _monitoring = require("@atlaskit/editor-common/monitoring");
var _events = require("../../analytics/events");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _uuid = _interopRequireDefault(require("uuid"));
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components
var ErrorBoundary = exports.ErrorBoundary = /*#__PURE__*/function (_React$Component) {
function ErrorBoundary() {
var _this;
(0, _classCallCheck2.default)(this, ErrorBoundary);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, ErrorBoundary, [].concat(args));
(0, _defineProperty2.default)(_this, "state", {
errorCaptured: false,
domError: false,
domErrorCount: 0
});
return _this;
}
(0, _inherits2.default)(ErrorBoundary, _React$Component);
return (0, _createClass2.default)(ErrorBoundary, [{
key: "fireAnalyticsEvent",
value: function fireAnalyticsEvent(event) {
var createAnalyticsEvent = this.props.createAnalyticsEvent;
if (createAnalyticsEvent) {
var channel = _types.FabricChannel.editor;
createAnalyticsEvent(event).fire(channel);
}
}
}, {
key: "hasFallback",
value: function hasFallback() {
return typeof this.props.fallbackComponent !== 'undefined';
}
}, {
key: "shouldRecover",
value: function shouldRecover() {
return this.hasFallback() && this.state.errorCaptured;
}
}, {
key: "componentDidCatch",
value: function componentDidCatch(error, errorInfo) {
var _this$props$additiona,
_this2 = this;
var additionalInfo = (_this$props$additiona = this.props.additionalInfo) !== null && _this$props$additiona !== void 0 ? _this$props$additiona : '';
this.fireAnalyticsEvent({
action: _analytics.ACTION.CRASHED,
actionSubject: this.props.component,
actionSubjectId: this.props.componentId,
eventType: _analytics.EVENT_TYPE.OPERATIONAL,
attributes: {
platform: _events.PLATFORM.WEB,
errorMessage: "".concat(additionalInfo).concat(error === null || error === void 0 ? void 0 : error.message),
componentStack: (errorInfo === null || errorInfo === void 0 ? void 0 : errorInfo.componentStack) || undefined,
errorRethrown: Boolean(this.props.rethrowError)
}
});
(0, _monitoring.logException)(error, {
location: 'renderer'
});
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var pattern = /Failed to execute.*on 'Node'.*/;
var matchesPattern = pattern.test(error.message);
if (matchesPattern) {
this.fireAnalyticsEvent({
action: _analytics.ACTION.CAUGHT_DOM_ERROR,
actionSubject: this.props.component,
actionSubjectId: this.props.componentId,
eventType: _analytics.EVENT_TYPE.OPERATIONAL,
attributes: {
platform: _events.PLATFORM.WEB,
errorMessage: "".concat(additionalInfo).concat(error === null || error === void 0 ? void 0 : error.message)
}
});
this.setState(function (prevState) {
return {
domError: true,
domErrorCount: prevState.domErrorCount + 1
};
});
}
if (this.hasFallback()) {
this.setState({
errorCaptured: true
}, function () {
if (_this2.props.rethrowError) {
throw error;
}
});
}
}
}, {
key: "render",
value: function render() {
if (this.state.domError) {
var key = (0, _expValEquals.expValEquals)('platform_editor_renderer_error_boundary_stable_key', 'isEnabled', true) ? this.state.domErrorCount :
// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
(0, _uuid.default)();
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
key: key
}, this.props.children);
}
if (this.shouldRecover()) {
return this.props.fallbackComponent;
}
return this.props.children;
}
}]);
}(_react.default.Component);