공통코드 관리 시스템 개선 완료
This commit is contained in:
+37
-13
@@ -66,8 +66,8 @@ apiClient.interceptors.request.use(
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
// 1순위: 전역 변수에서 확인
|
||||
if ((window as any).__GLOBAL_USER_LANG) {
|
||||
currentLang = (window as any).__GLOBAL_USER_LANG;
|
||||
if ((window as unknown as { __GLOBAL_USER_LANG?: string }).__GLOBAL_USER_LANG) {
|
||||
currentLang = (window as unknown as { __GLOBAL_USER_LANG: string }).__GLOBAL_USER_LANG;
|
||||
}
|
||||
// 2순위: localStorage에서 확인 (새 창이나 페이지 새로고침 시)
|
||||
else {
|
||||
@@ -80,7 +80,7 @@ apiClient.interceptors.request.use(
|
||||
|
||||
console.log("🌐 API 요청 시 언어 정보:", {
|
||||
currentLang,
|
||||
globalVar: (window as any).__GLOBAL_USER_LANG,
|
||||
globalVar: (window as unknown as { __GLOBAL_USER_LANG?: string }).__GLOBAL_USER_LANG,
|
||||
localStorage: typeof window !== "undefined" ? localStorage.getItem("userLocale") : null,
|
||||
url: config.url,
|
||||
});
|
||||
@@ -109,19 +109,39 @@ apiClient.interceptors.response.use(
|
||||
return response;
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
const status = error.response?.status;
|
||||
const url = error.config?.url;
|
||||
|
||||
// 409 에러 (중복 데이터)는 조용하게 처리
|
||||
if (status === 409) {
|
||||
// 중복 검사 API는 완전히 조용하게 처리
|
||||
if (url?.includes("/check-duplicate")) {
|
||||
// 중복 검사는 정상적인 비즈니스 로직이므로 콘솔 출력 없음
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
// 일반 409 에러는 간단한 로그만 출력
|
||||
console.warn("⚠️ 데이터 중복:", {
|
||||
url: url,
|
||||
message: (error.response?.data as { message?: string })?.message || "중복된 데이터입니다.",
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
// 다른 에러들은 기존처럼 상세 로그 출력
|
||||
console.error("❌ API 응답 오류:", {
|
||||
status: error.response?.status,
|
||||
status: status,
|
||||
statusText: error.response?.statusText,
|
||||
url: error.config?.url,
|
||||
url: url,
|
||||
data: error.response?.data,
|
||||
message: error.message,
|
||||
headers: error.config?.headers,
|
||||
});
|
||||
|
||||
// 401 에러 시 상세 정보 출력
|
||||
if (error.response?.status === 401) {
|
||||
if (status === 401) {
|
||||
console.error("🚨 401 Unauthorized 오류 상세 정보:", {
|
||||
url: error.config?.url,
|
||||
url: url,
|
||||
method: error.config?.method,
|
||||
headers: error.config?.headers,
|
||||
requestData: error.config?.data,
|
||||
@@ -131,7 +151,7 @@ apiClient.interceptors.response.use(
|
||||
}
|
||||
|
||||
// 401 에러 시 토큰 제거 및 로그인 페이지로 리다이렉트
|
||||
if (error.response?.status === 401 && typeof window !== "undefined") {
|
||||
if (status === 401 && typeof window !== "undefined") {
|
||||
console.log("🔄 401 에러 감지 - 토큰 제거 및 로그인 페이지로 리다이렉트");
|
||||
localStorage.removeItem("authToken");
|
||||
|
||||
@@ -146,7 +166,7 @@ apiClient.interceptors.response.use(
|
||||
);
|
||||
|
||||
// 공통 응답 타입
|
||||
export interface ApiResponse<T = any> {
|
||||
export interface ApiResponse<T = unknown> {
|
||||
success: boolean;
|
||||
data?: T;
|
||||
message?: string;
|
||||
@@ -157,7 +177,7 @@ export interface ApiResponse<T = any> {
|
||||
export const apiCall = async <T>(
|
||||
method: "GET" | "POST" | "PUT" | "DELETE",
|
||||
url: string,
|
||||
data?: any,
|
||||
data?: unknown,
|
||||
): Promise<ApiResponse<T>> => {
|
||||
try {
|
||||
const response = await apiClient.request({
|
||||
@@ -166,12 +186,16 @@ export const apiCall = async <T>(
|
||||
data,
|
||||
});
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
console.error("API 호출 실패:", error);
|
||||
const axiosError = error as AxiosError;
|
||||
return {
|
||||
success: false,
|
||||
message: error.response?.data?.message || error.message || "알 수 없는 오류가 발생했습니다.",
|
||||
errorCode: error.response?.data?.errorCode,
|
||||
message:
|
||||
(axiosError.response?.data as { message?: string })?.message ||
|
||||
axiosError.message ||
|
||||
"알 수 없는 오류가 발생했습니다.",
|
||||
errorCode: (axiosError.response?.data as { errorCode?: string })?.errorCode,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user