feat: add error catcher component

This commit is contained in:
Tristan Yang
2023-03-14 22:06:06 +08:00
parent 5bc13c6e58
commit f038c683b0
3 changed files with 33 additions and 2 deletions
+27
View File
@@ -0,0 +1,27 @@
import React, { ReactNode } from 'react';
import { ErrorBoundary } from "react-error-boundary";
type FallbackProps = {
error: Error,
resetErrorBoundary: (...args: Array<unknown>) => void
}
function ErrorFallback({ error, resetErrorBoundary }: FallbackProps) {
return (
<div role="alert" className='text-lg text-red-600'>
<p>Something went wrong:</p>
<pre>{error.message}</pre>
<button onClick={resetErrorBoundary}>Try again</button>
</div>
);
}
type Props = {
children: ReactNode
}
const ErrorCatcher = ({ children }: Props) => {
return (
<ErrorBoundary FallbackComponent={ErrorFallback}>{children}</ErrorBoundary>
);
};
export default ErrorCatcher;
@@ -5,6 +5,7 @@ import { Nav } from "../../routes/settingChannel/navs";
import clsx from "clsx"; import clsx from "clsx";
import GoBackNav from "./GoBackNav"; import GoBackNav from "./GoBackNav";
import MobileNavs from "../../routes/home/MobileNavs"; import MobileNavs from "../../routes/home/MobileNavs";
// import ErrorCatcher from "./ErrorCatcher";
export interface Danger { export interface Danger {
title: string; title: string;
handler: () => void; handler: () => void;
@@ -69,7 +70,9 @@ const StyledSettingContainer: FC<PropsWithChildren<Props>> = ({
<div className={clsx("relative bg-white w-full max-h-full overflow-auto px-4 md:px-8 py-2 md:py-8 dark:bg-gray-700", !nav ? "hidden md:block" : "!pb-4")}> <div className={clsx("relative bg-white w-full max-h-full overflow-auto px-4 md:px-8 py-2 md:py-8 dark:bg-gray-700", !nav ? "hidden md:block" : "!pb-4")}>
<GoBackNav path={pathPrefix} className="!left-1 top-1.5" /> <GoBackNav path={pathPrefix} className="!left-1 top-1.5" />
{nav && <h4 className="font-bold text-xl text-center md:text-left text-gray-600 mb-4 md:mb-8 pl-4 md:pl-0 dark:text-gray-100">{nav.title}</h4>} {nav && <h4 className="font-bold text-xl text-center md:text-left text-gray-600 mb-4 md:mb-8 pl-4 md:pl-0 dark:text-gray-100">{nav.title}</h4>}
{/* <ErrorCatcher> */}
{children} {children}
{/* </ErrorCatcher> */}
</div> </div>
</div> </div>
{!nav && <MobileNavs />} {!nav && <MobileNavs />}
+3 -2
View File
@@ -13,6 +13,7 @@ import { useAppSelector } from "../../app/store";
import GuestBlankPlaceholder from "./GuestBlankPlaceholder"; import GuestBlankPlaceholder from "./GuestBlankPlaceholder";
import GuestChannelChat from "./GuestChannelChat"; import GuestChannelChat from "./GuestChannelChat";
import GuestSessionList from "./GuestSessionList"; import GuestSessionList from "./GuestSessionList";
import ErrorCatcher from "../../common/component/ErrorCatcher";
function ChatPage() { function ChatPage() {
const isHomePath = useMatch(`/`); const isHomePath = useMatch(`/`);
@@ -49,7 +50,7 @@ function ChatPage() {
const placeholderVisible = channel_id == 0 && user_id == 0; const placeholderVisible = channel_id == 0 && user_id == 0;
const isMainPath = isHomePath || isChatHomePath; const isMainPath = isHomePath || isChatHomePath;
return ( return (
<> <ErrorCatcher>
{channelModalVisible && ( {channelModalVisible && (
<ChannelModal closeModal={toggleChannelModalVisible} personal={true} /> <ChannelModal closeModal={toggleChannelModalVisible} personal={true} />
)} )}
@@ -76,7 +77,7 @@ function ChatPage() {
{user_id !== 0 && <DMChat uid={+user_id} />} {user_id !== 0 && <DMChat uid={+user_id} />}
</div> </div>
</div> </div>
</> </ErrorCatcher>
); );
} }
export default memo(ChatPage); export default memo(ChatPage);