error.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import React from "react";
  2. import { IconButton } from "./button";
  3. import GithubIcon from "../icons/github.svg";
  4. import { ISSUE_URL } from "../constant";
  5. interface IErrorBoundaryState {
  6. hasError: boolean;
  7. error: Error | null;
  8. info: React.ErrorInfo | null;
  9. }
  10. export class ErrorBoundary extends React.Component<any, IErrorBoundaryState> {
  11. constructor(props: any) {
  12. super(props);
  13. this.state = { hasError: false, error: null, info: null };
  14. }
  15. componentDidCatch(error: Error, info: React.ErrorInfo) {
  16. // Update state with error details
  17. this.setState({ hasError: true, error, info });
  18. }
  19. render() {
  20. if (this.state.hasError) {
  21. // Render error message
  22. return (
  23. <div className="error">
  24. <h2>Oops, something went wrong!</h2>
  25. <pre>
  26. <code>{this.state.error?.toString()}</code>
  27. <code>{this.state.info?.componentStack}</code>
  28. </pre>
  29. <a href={ISSUE_URL} className="report">
  30. <IconButton
  31. text="Report This Error"
  32. icon={<GithubIcon />}
  33. bordered
  34. />
  35. </a>
  36. </div>
  37. );
  38. }
  39. // if no error occurred, render children
  40. return this.props.children;
  41. }
  42. }