Skip to content

Commit

Permalink
Add: ErrorBoundary
Browse files Browse the repository at this point in the history
  • Loading branch information
shm11C3 committed Oct 5, 2024
1 parent a5d5a4f commit 445bde2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
31 changes: 31 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"react": "^18.3.1",
"react-chartjs-2": "^5.2.0",
"react-dom": "^18.3.1",
"react-error-boundary": "^4.0.13",
"react-hook-form": "^7.53.0",
"tailwind-merge": "^2.5.2",
"tailwind-variants": "^0.2.1",
Expand Down
24 changes: 17 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import {
useErrorModalListener,
useSettingsModalListener,
} from "@/hooks/useTauriEventListener";
import { useAtom } from "jotai";
import { selectedMenuAtom } from "./atom/ui";
import type { ErrorInfo } from "react";
import { ErrorBoundary } from "react-error-boundary";
import { useSettingsAtom } from "./atom/useSettingsAtom";
import ErrorFallback from "./components/ErrorFallback";
import { useDarkMode } from "./hooks/useDarkMode";
import ScreenTemplate from "./template/ScreenTemplate";
import Settings from "./template/Settings";
import SideMenu from "./template/SideMenu";
import type { SelectedMenuType } from "./types/ui";

const onError = (error: Error, info: ErrorInfo) => {
console.log("error.message", error.message);
console.log(
"info.componentStack:",
info.componentStack ?? "No stack trace available",
);
};

const Page = () => {
const { settings } = useSettingsAtom();
Expand Down Expand Up @@ -50,10 +58,12 @@ const Page = () => {
};

return (
<div className="bg-slate-200 dark:bg-gray-900 text-gray-900 dark:text-white min-h-screen">
<SideMenu />
{displayTargets[selectedMenu]}
</div>
<ErrorBoundary FallbackComponent={ErrorFallback} onError={onError}>
<div className="bg-slate-200 dark:bg-gray-900 text-gray-900 dark:text-white min-h-screen">
<SideMenu />
{displayTargets[settings.state.display]}

Check failure on line 64 in src/App.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

Property 'state' does not exist on type 'Settings'.
</div>
</ErrorBoundary>
);
};

Expand Down
11 changes: 11 additions & 0 deletions src/components/ErrorFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { FallbackProps } from "react-error-boundary";

function ErrorFallback({ error }: FallbackProps) {
return (
<div>
<h2>An unexpected error has occurred.</h2>
<pre>{error.message}</pre>
</div>
);
}
export default ErrorFallback;

0 comments on commit 445bde2

Please sign in to comment.