about summary refs log tree commit diff
path: root/src/view/com/util/ErrorBoundary.tsx
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-01-02 21:49:14 -0600
committerPaul Frazee <pfrazee@gmail.com>2023-01-02 21:49:14 -0600
commitd262393992f73e22f750f903da58cd8e05344f89 (patch)
treeee8b7db94eadbea2c1044c2580e82d3bfe389d61 /src/view/com/util/ErrorBoundary.tsx
parent4eabc2d65aa742e6cf55822943f04275531d0375 (diff)
downloadvoidsky-d262393992f73e22f750f903da58cd8e05344f89.tar.zst
Introduce error boundaries around all screens
Diffstat (limited to 'src/view/com/util/ErrorBoundary.tsx')
-rw-r--r--src/view/com/util/ErrorBoundary.tsx40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/view/com/util/ErrorBoundary.tsx b/src/view/com/util/ErrorBoundary.tsx
new file mode 100644
index 000000000..017265f48
--- /dev/null
+++ b/src/view/com/util/ErrorBoundary.tsx
@@ -0,0 +1,40 @@
+import React, {Component, ErrorInfo, ReactNode} from 'react'
+import {ErrorScreen} from './error/ErrorScreen'
+
+interface Props {
+  children?: ReactNode
+}
+
+interface State {
+  hasError: boolean
+  error: any
+}
+
+export class ErrorBoundary extends Component<Props, State> {
+  public state: State = {
+    hasError: false,
+    error: undefined,
+  }
+
+  public static getDerivedStateFromError(error: Error): State {
+    return {hasError: true, error}
+  }
+
+  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
+    console.error('Uncaught error:', error, errorInfo)
+  }
+
+  public render() {
+    if (this.state.hasError) {
+      return (
+        <ErrorScreen
+          title="Oh no!"
+          message="There was an unexpected issue in the application. Please let us know if this happened to you!"
+          details={this.state.error.toString()}
+        />
+      )
+    }
+
+    return this.props.children
+  }
+}