Skip to content

Commit

Permalink
add error boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 12, 2024
1 parent 89e5d7c commit 7902f92
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"pubsub-js": "^1.9.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-error-boundary": "^4.0.13",
"react-icons": "^5.2.1",
"react-monaco-editor": "^0.56.0",
"react-router-dom": "^6.26.0",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions src/renderer/src/components/base/base-error-boundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Button } from '@nextui-org/react'
import { ReactNode } from 'react'
import { ErrorBoundary, FallbackProps } from 'react-error-boundary'

const ErrorFallback = ({ error }: FallbackProps): JSX.Element => {
return (
<div className="p-4">
<h2 className="my-2 text-lg font-bold">
{'应用崩溃了 :( 请将以下信息提交给开发者以排查错误'}
</h2>

<Button
size="sm"
color="primary"
variant="flat"
onPress={() => open('https://github.com/pompurin404/mihomo-party/issues/new/choose')}
>
GitHub
</Button>
<Button
size="sm"
color="primary"
variant="flat"
className="ml-2"
onPress={() => open('https://t.me/mihomo_party')}
>
Telegram
</Button>

<Button
size="sm"
variant="flat"
className="ml-2"
onPress={() =>
navigator.clipboard.writeText('```\n' + error.message + '\n' + error.stack + '\n```')
}
>
复制报错信息
</Button>

<p className="my-2">{error.message}</p>

<details title="Error Stack">
<summary>Error Stack</summary>
<pre>{error.stack}</pre>
</details>
</div>
)
}

interface Props {
children?: ReactNode
}

const BaseErrorBoundary = (props: Props): JSX.Element => {
return <ErrorBoundary FallbackComponent={ErrorFallback}>{props.children}</ErrorBoundary>
}

export default BaseErrorBoundary
9 changes: 6 additions & 3 deletions src/renderer/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NextUIProvider } from '@nextui-org/react'
import { init } from '@renderer/utils/init'
import '@renderer/assets/main.css'
import App from '@renderer/App'
import BaseErrorBoundary from './components/base/base-error-boundary'

init().then(() => {
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
Expand All @@ -17,9 +18,11 @@ init().then(() => {
enableSystem
defaultTheme="dark"
>
<HashRouter>
<App />
</HashRouter>
<BaseErrorBoundary>
<HashRouter>
<App />
</HashRouter>
</BaseErrorBoundary>
</NextThemesProvider>
</NextUIProvider>
</React.StrictMode>
Expand Down

0 comments on commit 7902f92

Please sign in to comment.