Skip to content

Commit cf4bf90

Browse files
committed
fix: enhance error handling in Axios interceptor and add RequiredLoginFallback component for better user experience
1 parent bafe10e commit cf4bf90

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

apps/ticket/src/lib/axios/clientAxios.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,12 @@ clientAxios.interceptors.response.use(
6161
} catch (e) {
6262
redirectToLoginOnce();
6363

64-
return;
64+
return Promise.reject(e);
6565
}
6666
}
6767
}
6868
}
6969

70-
// 로그인이 필요한 요청이거나, 리프레시 토큰 모두 만료시 로그인 페이지로 이동
71-
if (isAxiosErrorResponse(error)) {
72-
const { code } = error.response?.data ?? {};
73-
74-
// 엑세스 토큰 없음
75-
if (code === ERROR_CODE.NO_ACCESS_TOKEN || code === ERROR_CODE.REFRESH_TOKEN_EXPIRED) {
76-
redirectToLoginOnce();
77-
78-
return;
79-
}
80-
81-
if (code === ERROR_CODE.LOGIN_REQUIRED) {
82-
// 인증 페이지에서는 로그인 페이지로 이동하지 않음
83-
if (window.location.pathname !== "/auth") {
84-
redirectToLoginOnce();
85-
86-
return;
87-
}
88-
}
89-
90-
if (code === ERROR_CODE.PAYMENT) {
91-
return Promise.reject(error?.response?.data);
92-
}
93-
}
94-
9570
if (error?.response?.data) {
9671
return Promise.reject({
9772
...error,

apps/ticket/src/shared/clientBoundary/ErrorBoundary/GlobalErrorBoundary/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { Suspense } from "react";
44

55
import { AuthErrorFallback } from "@/shared/components/AuthErrorFallback";
66
import { NetworkErrorFallback } from "@/shared/components/NetworkErrorFallback";
7-
import { isAuthError, isNetworkError } from "@/shared/types/axioxError";
7+
import { RequiredLoginFallback } from "@/shared/components/RequiredLoginFallback";
8+
import { isAuthError, isNetworkError, isNotAuthErrorResponse } from "@/shared/types/axioxError";
89

910
import { ErrorBoundary, ErrorHandler } from "..";
1011
import { GlobalErrorPage } from "./components/GlobalErrorPage";
1112

1213
export const GlobalErrorBoundary = ({ children }: { children: React.ReactNode }) => (
13-
<ErrorBoundary handlers={[authErrorHandler, networkErrorHandler, globalErrorHandler]}>
14+
<ErrorBoundary
15+
handlers={[authErrorHandler, requiredLoginHandler, networkErrorHandler, globalErrorHandler]}
16+
>
1417
<Suspense>{children}</Suspense>
1518
</ErrorBoundary>
1619
);
@@ -20,6 +23,11 @@ const authErrorHandler: ErrorHandler = {
2023
fallback: () => <AuthErrorFallback />,
2124
};
2225

26+
const requiredLoginHandler: ErrorHandler = {
27+
isError: (error) => isNotAuthErrorResponse(error),
28+
fallback: () => <RequiredLoginFallback />,
29+
};
30+
2331
const networkErrorHandler: ErrorHandler = {
2432
isError: (error) => isNetworkError(error),
2533
fallback: () => <NetworkErrorFallback />,

apps/ticket/src/shared/components/NetworkErrorFallback/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const NetworkErrorFallback = () => {
1212
gap={16}
1313
style={{ height: "calc(100vh - 150px)" }}
1414
>
15-
<Typography type="title24">서비스에 문제가 발생했어요</Typography>
15+
<Typography type="title20">네트워크 에러가 발생했어요</Typography>
1616
<Typography type="body14">잠시 후 다시 시도해주세요</Typography>
1717
<Button
1818
variant="primary"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use client";
2+
3+
import { useEffect } from "react";
4+
5+
import { Flex, Typography } from "@permit/design-system";
6+
import { safeLocalStorage } from "@/lib/storage";
7+
import { PATH } from "@/shared/constants/path";
8+
import { IS_LOGINED } from "@/shared/constants/storage";
9+
10+
// TODO: 추후 변경
11+
export const RequiredLoginFallback = () => {
12+
useEffect(() => {
13+
function redirectToLogin() {
14+
alert("로그인 후 이용해 주세요.");
15+
safeLocalStorage.remove(IS_LOGINED);
16+
17+
const redirectUrl = window.location.pathname + window.location.search;
18+
19+
const loginUrl = `${PATH.LOGIN}?redirectUrl=${encodeURIComponent(redirectUrl)}`;
20+
21+
window.location.href = loginUrl;
22+
}
23+
24+
redirectToLogin();
25+
}, []);
26+
27+
return (
28+
<Flex
29+
direction="column"
30+
align="center"
31+
justify="center"
32+
gap={16}
33+
style={{ height: "calc(100vh" }}
34+
>
35+
<Typography type="title20">로그인 화면으로 이동 중이에요</Typography>
36+
<Typography type="body14">잠시만 기다려주세요</Typography>
37+
</Flex>
38+
);
39+
};

apps/ticket/src/shared/types/axioxError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export function isAxiosErrorResponse(error: unknown): error is AxiosErrorRespons
2727
export function isNotAuthErrorResponse(error: unknown): error is AxiosErrorResponse {
2828
return (
2929
isAxiosErrorResponse(error) &&
30-
(error.response?.data.code === ERROR_CODE.NO_ACCESS_TOKEN ||
30+
(error.response?.data.code === ERROR_CODE.LOGIN_REQUIRED ||
31+
error.response?.data.code === ERROR_CODE.NO_ACCESS_TOKEN ||
3132
error.response?.data.code === ERROR_CODE.REFRESH_TOKEN_EXPIRED)
3233
);
3334
}

0 commit comments

Comments
 (0)