Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fe/feature/#560 error boundary적용 #564

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"html2canvas": "^1.4.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-error-boundary": "^4.0.12",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 라이브러리가 있구나!!

"react-router-dom": "^6.20.1",
"socket.io-client": "^4.7.2",
"zustand": "^4.4.7"
Expand Down
14 changes: 12 additions & 2 deletions frontend/pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { RouterProvider } from 'react-router-dom';
import { rootRouter } from 'rootRouter';
import { router } from 'router';

import { BackgroundMusic, Cursor } from '@components/common';

export function App() {
return (
<>
<Cursor />
<RouterProvider router={rootRouter} />
<RouterProvider router={router} />
<BackgroundMusic />
</>
);
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/business/services/SocketManager/SocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export class SocketManager<T1 extends EventsMap, T2 extends EventsMap> {
return;
}
this.#socket = io(this.#url, { path: this.#path, withCredentials });

this.socket?.on('disconnect', () => {
alert('서버와 연결이 끊겼습니다. 메인 페이지로 이동합니다.');
window.location.href = '/';
});

this.socket?.on('connect_error', () => {
alert('서버와 연결할 수 없습니다. 메인 페이지로 이동합니다.');
window.location.href = '/';
});
Comment on lines +33 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍

}

disconnect() {
Expand Down
41 changes: 41 additions & 0 deletions frontend/src/errors/APIErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useQueryErrorResetBoundary } from '@tanstack/react-query';
import { isAxiosError } from 'axios';
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';

import { SomethingWrongErrorPage } from '@pages/ErrorPage';

const APIErrorFallback = ({ error }: FallbackProps) => {
if (
isAxiosError<{
errorCode: string;
message: string;
}>(error)
) {
const responseBody = error.response?.data;

// TODO: 에러코드 정의되면 여기서 에러 코드에 따른 작업을 해줘야함.
switch (responseBody?.errorCode) {
}

// 에러 코드에 따른 작업 처리후 에러 페이지로 이동
return <SomethingWrongErrorPage />;
} else {
// 그 외의 에러의 경우 상단 바운더리에서 처리
throw error;
}
};

export function APIErrorBoundary({ children }: { children: React.ReactNode }) {
const { reset } = useQueryErrorResetBoundary();

return (
<ErrorBoundary
FallbackComponent={APIErrorFallback}
onReset={reset}
>
{children}
</ErrorBoundary>
);
}

export default APIErrorBoundary;
20 changes: 20 additions & 0 deletions frontend/src/errors/UnknownErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useQueryErrorResetBoundary } from '@tanstack/react-query';
import type { PropsWithChildren } from 'react';
import { ErrorBoundary } from 'react-error-boundary';

import { SomethingWrongErrorPage } from '@pages/ErrorPage';

export function UnknownErrorBoundary({ children }: PropsWithChildren) {
const { reset } = useQueryErrorResetBoundary();

return (
<ErrorBoundary
FallbackComponent={SomethingWrongErrorPage}
onReset={reset}
>
{children}
</ErrorBoundary>
);
}

export default UnknownErrorBoundary;
2 changes: 2 additions & 0 deletions frontend/src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './APIErrorBoundary';
export * from './UnknownErrorBoundary';
8 changes: 8 additions & 0 deletions frontend/src/pages/ErrorPage/SomethingWrongErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function SomethingWrongErrorPage() {
return (
<div>
<h1>에러가 발생했습니다.</h1>
<p>잠시 후 다시 시도해주세요.</p>
</div>
);
}
1 change: 1 addition & 0 deletions frontend/src/pages/ErrorPage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SomethingWrongErrorPage';
17 changes: 14 additions & 3 deletions frontend/src/rootRouter.tsx → frontend/src/router.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Route, createBrowserRouter, createRoutesFromElements } from 'react-router-dom';
import { UnknownErrorBoundary } from 'errors';
import APIErrorBoundary from 'errors/APIErrorBoundary';
import { Outlet, Route, createBrowserRouter, createRoutesFromElements } from 'react-router-dom';

import {
AIChatPage,
Expand All @@ -10,9 +12,18 @@ import {
SettingPage,
} from './pages';

export const rootRouter = createBrowserRouter(
export const router = createBrowserRouter(
createRoutesFromElements(
<Route path="/">
<Route
path="/"
element={
<UnknownErrorBoundary>
<APIErrorBoundary>
<Outlet />
</APIErrorBoundary>
</UnknownErrorBoundary>
}
>
<Route
index
path=""
Expand Down
Loading