UNPKG

@grafana/ui

Version:
92 lines (87 loc) 3.14 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var jsxRuntime = require('react/jsx-runtime'); var React = require('react'); var faroWebSdk = require('@grafana/faro-web-sdk'); var i18n = require('@grafana/i18n'); var Alert = require('../Alert/Alert.cjs'); var ErrorWithStack = require('./ErrorWithStack.cjs'); "use strict"; class ErrorBoundary extends React.PureComponent { constructor() { super(...arguments); this.state = { error: null, errorInfo: null }; } componentDidCatch(error, errorInfo) { var _a, _b, _c; if (this.props.errorLogger) { this.props.errorLogger(error); } else { (_c = (_a = faroWebSdk.faro) == null ? void 0 : _a.api) == null ? void 0 : _c.pushError(error, { context: { type: "boundary", source: (_b = this.props.boundaryName) != null ? _b : "unknown" } }); } this.setState({ error, errorInfo }); if (this.props.onError) { this.props.onError(error); } } componentDidUpdate(prevProps) { const { dependencies, onRecover } = this.props; if (this.state.error) { if (dependencies && prevProps.dependencies) { for (let i = 0; i < dependencies.length; i++) { if (dependencies[i] !== prevProps.dependencies[i]) { this.setState({ error: null, errorInfo: null }); if (onRecover) { onRecover(); } break; } } } } } render() { const { children } = this.props; const { error, errorInfo } = this.state; return children({ error, errorInfo }); } } const ErrorBoundaryAlert = React.memo( ({ title, children, style = "alertbox", dependencies, errorLogger, boundaryName }) => { const alertTitle = title != null ? title : i18n.t("grafana-ui.error-boundary.title", "An unexpected error happened"); return /* @__PURE__ */ jsxRuntime.jsx(ErrorBoundary, { dependencies, errorLogger, boundaryName, children: ({ error, errorInfo }) => { if (!errorInfo) { return children; } if (style === "alertbox") { return /* @__PURE__ */ jsxRuntime.jsx(Alert.Alert, { title: alertTitle, children: /* @__PURE__ */ jsxRuntime.jsxs("details", { style: { whiteSpace: "pre-wrap" }, children: [ error && error.toString(), /* @__PURE__ */ jsxRuntime.jsx("br", {}), errorInfo.componentStack ] }) }); } return /* @__PURE__ */ jsxRuntime.jsx(ErrorWithStack.ErrorWithStack, { title: alertTitle, error, errorInfo }); } }); } ); ErrorBoundaryAlert.displayName = "ErrorBoundaryAlert"; function withErrorBoundary(Component, errorBoundaryProps = {}) { const comp = (props) => /* @__PURE__ */ jsxRuntime.jsx(ErrorBoundaryAlert, { ...errorBoundaryProps, children: /* @__PURE__ */ jsxRuntime.jsx(Component, { ...props }) }); comp.displayName = "WithErrorBoundary"; return comp; } exports.ErrorBoundary = ErrorBoundary; exports.ErrorBoundaryAlert = ErrorBoundaryAlert; exports.withErrorBoundary = withErrorBoundary; //# sourceMappingURL=ErrorBoundary.cjs.map