UNPKG

@atlaskit/renderer

Version:
124 lines 5.23 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(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; })(); } import React from 'react'; import { ACTION, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { FabricChannel } from '@atlaskit/analytics-listeners/types'; import { logException } from '@atlaskit/editor-common/monitoring'; import { PLATFORM } from '../../analytics/events'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead import uuid from 'uuid'; // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components export var ErrorBoundary = /*#__PURE__*/function (_React$Component) { function ErrorBoundary() { var _this; _classCallCheck(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)); _defineProperty(_this, "state", { errorCaptured: false, domError: false, domErrorCount: 0 }); return _this; } _inherits(ErrorBoundary, _React$Component); return _createClass(ErrorBoundary, [{ key: "fireAnalyticsEvent", value: function fireAnalyticsEvent(event) { var createAnalyticsEvent = this.props.createAnalyticsEvent; if (createAnalyticsEvent) { var channel = 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: ACTION.CRASHED, actionSubject: this.props.component, actionSubjectId: this.props.componentId, eventType: EVENT_TYPE.OPERATIONAL, attributes: { platform: 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) } }); 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: ACTION.CAUGHT_DOM_ERROR, actionSubject: this.props.component, actionSubjectId: this.props.componentId, eventType: EVENT_TYPE.OPERATIONAL, attributes: { platform: 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 = 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 uuid(); return /*#__PURE__*/React.createElement(React.Fragment, { key: key }, this.props.children); } if (this.shouldRecover()) { return this.props.fallbackComponent; } return this.props.children; } }]); }(React.Component);