ErrorBoundary.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use client";
  2. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  3. import _createClass from "@babel/runtime/helpers/esm/createClass";
  4. import _callSuper from "@babel/runtime/helpers/esm/callSuper";
  5. import _inherits from "@babel/runtime/helpers/esm/inherits";
  6. import * as React from 'react';
  7. import Alert from './Alert';
  8. let ErrorBoundary = /*#__PURE__*/function (_React$Component) {
  9. function ErrorBoundary() {
  10. var _this;
  11. _classCallCheck(this, ErrorBoundary);
  12. _this = _callSuper(this, ErrorBoundary, arguments);
  13. _this.state = {
  14. error: undefined,
  15. info: {
  16. componentStack: ''
  17. }
  18. };
  19. return _this;
  20. }
  21. _inherits(ErrorBoundary, _React$Component);
  22. return _createClass(ErrorBoundary, [{
  23. key: "componentDidCatch",
  24. value: function componentDidCatch(error, info) {
  25. this.setState({
  26. error,
  27. info
  28. });
  29. }
  30. }, {
  31. key: "render",
  32. value: function render() {
  33. const {
  34. message,
  35. description,
  36. id,
  37. children
  38. } = this.props;
  39. const {
  40. error,
  41. info
  42. } = this.state;
  43. const componentStack = (info === null || info === void 0 ? void 0 : info.componentStack) || null;
  44. const errorMessage = typeof message === 'undefined' ? (error || '').toString() : message;
  45. const errorDescription = typeof description === 'undefined' ? componentStack : description;
  46. if (error) {
  47. return /*#__PURE__*/React.createElement(Alert, {
  48. id: id,
  49. type: "error",
  50. message: errorMessage,
  51. description: /*#__PURE__*/React.createElement("pre", {
  52. style: {
  53. fontSize: '0.9em',
  54. overflowX: 'auto'
  55. }
  56. }, errorDescription)
  57. });
  58. }
  59. return children;
  60. }
  61. }]);
  62. }(React.Component);
  63. export default ErrorBoundary;