Skip to content

Let's use AI to Earn!

feat: 错误捕获完成

gaoSirb7763645f372
diff --git a/electron/main/index.ts b/electron/main/index.ts
index f841c6b8..f2f13a28 100644
--- a/electron/main/index.ts
+++ b/electron/main/index.ts
@@ -60,7 +60,7 @@ async function createWindow() {
titleBarOverlay: {
color: 'rgba(0,0,0,0)',
height: 64,
- symbolColor: '#000',
+ symbolColor: '#595959',
},
webPreferences: {
preload,
diff --git a/src/components/ErrorBoundary/ErrorBoundary.tsx b/src/components/ErrorBoundary/ErrorBoundary.tsx
new file mode 100644
index 00000000..cf155f31
--- /dev/null
+++ b/src/components/ErrorBoundary/ErrorBoundary.tsx
@@ -0,0 +1,54 @@
+import React, { useState, useEffect } from 'react';
+import styles from './errorBoundary.module.scss';
+import { Button } from 'antd';
+import { useNavigate } from 'react-router-dom';
+
+interface ErrorBoundaryProps {
+ children: React.ReactNode;
+}
+
+const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ children }) => {
+ const [hasError, setHasError] = useState(false);
+ const [errorMsg, setErrorMsg] = useState('');
+ const navigate = useNavigate();
+
+ const handleError = (error: Error) => {
+ setErrorMsg(error.stack || '');
+ setHasError(true);
+ };
+
+ useEffect(() => {
+ const errorListener = (event: ErrorEvent) => {
+ handleError(event.error);
+ };
+
+ window.addEventListener('error', errorListener);
+
+ return () => {
+ window.removeEventListener('error', errorListener);
+ };
+ }, []);
+
+ if (hasError) {
+ // 您可以自定义回退UI
+ return (
+ <div className={styles.errorBoundary}>
+ <h1>对不起,系统出现了错误</h1>
+ <Button
+ type="primary"
+ onClick={() => {
+ console.log(errorMsg);
+ console.log('错误上报');
+ navigate('/login');
+ }}
+ >
+ 上报错误
+ </Button>
+ </div>
+ );
+ }
+
+ return <>{children}</>;
+};
+
+export default ErrorBoundary;
diff --git a/src/components/ErrorBoundary/errorBoundary.module.scss b/src/components/ErrorBoundary/errorBoundary.module.scss
new file mode 100644
index 00000000..bf90f23a
--- /dev/null
+++ b/src/components/ErrorBoundary/errorBoundary.module.scss
@@ -0,0 +1,26 @@
+.errorBoundary {
+ position: relative;
+ height: 100vh;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ flex-direction: column;
+ &:after {
+ content: "";
+ display: block;
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 64px;
+ user-select: none;
+ -webkit-app-region: drag;
+ }
+
+ :global {
+ h1 {
+ font-size: var(--fs-xl);
+ margin-bottom: 20px;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/layout/Navigation/navigation.module.scss b/src/layout/Navigation/navigation.module.scss
index 639b360a..b6abc48e 100644
--- a/src/layout/Navigation/navigation.module.scss
+++ b/src/layout/Navigation/navigation.module.scss
@@ -69,4 +69,3 @@
.navigation-darwin {
}
-
diff --git a/src/router/index.tsx b/src/router/index.tsx
index 6ab3afd7..c02427ec 100644
--- a/src/router/index.tsx
+++ b/src/router/index.tsx
@@ -46,6 +46,7 @@ import UserWalletAccount from '@/views/finance/userWalletAccount';
import Finance from '@/views/finance/index';
import UserWalletRecord from '@/views/finance/userWalletRecord';
import Reply from '@/views/reply';
+import ErrorBoundary from '../components/ErrorBoundary/ErrorBoundary';
interface IRouterMeta {
// 路由名称
@@ -76,7 +77,11 @@ type CustomRouteObject =
*/
export const router: CustomRouteObject[] = [
{
- element: <LayoutBody />,
+ element: (
+ <ErrorBoundary>
+ <LayoutBody />
+ </ErrorBoundary>
+ ),
children: [
// 重定向 ---------
{ path: '/publish', element: <Navigate to="/publish/video" /> },
@@ -105,7 +110,10 @@ export const router: CustomRouteObject[] = [
element: <Publish />,
meta: { name: '一键发布', icon: CopyOutlined },
children: [
- { path: 'video', element: <VideoPage /> },
+ {
+ path: 'video',
+ element: <VideoPage />,
+ },
{
path: 'image',
element: <ImagePage />,