@@ -1,11 +1,11 @@
|
||||
import axios from "axios";
|
||||
import axios from 'axios';
|
||||
|
||||
import Spotlight from "@enact/spotlight";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { useState } from "react";
|
||||
import { fetchCurrentUserHomeTermsSafe } from "../actions/homeActions";
|
||||
import Spotlight from '@enact/spotlight';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { useState } from 'react';
|
||||
import { fetchCurrentUserHomeTermsSafe } from '../actions/homeActions';
|
||||
|
||||
import { types } from "../actions/actionTypes";
|
||||
import { types } from '../actions/actionTypes';
|
||||
import {
|
||||
changeAppStatus,
|
||||
changeLocalSettings,
|
||||
@@ -13,16 +13,22 @@ import {
|
||||
setSystemNotice,
|
||||
setSystemTermination,
|
||||
showError,
|
||||
} from "../actions/commonActions";
|
||||
} from '../actions/commonActions';
|
||||
import {
|
||||
getAuthenticationCode,
|
||||
getReAuthenticationCode,
|
||||
} from "../actions/deviceActions";
|
||||
import { pushPanel, resetPanels } from "../actions/panelActions";
|
||||
import * as Config from "../utils/Config";
|
||||
import { ACTIVE_POPUP } from "../utils/Config";
|
||||
import * as HelperMethods from "../utils/helperMethods";
|
||||
import { getUrl, URLS } from "./apiConfig";
|
||||
} from '../actions/deviceActions';
|
||||
import {
|
||||
pushPanel,
|
||||
resetPanels,
|
||||
} from '../actions/panelActions';
|
||||
import * as Config from '../utils/Config';
|
||||
import { ACTIVE_POPUP } from '../utils/Config';
|
||||
import * as HelperMethods from '../utils/helperMethods';
|
||||
import {
|
||||
getUrl,
|
||||
URLS,
|
||||
} from './apiConfig';
|
||||
|
||||
let tokenRefreshing = false;
|
||||
const axiosQueue = [];
|
||||
@@ -201,23 +207,8 @@ export const TAxios = (
|
||||
if (onFail) onFail(res);
|
||||
return;
|
||||
}
|
||||
// 🔍 DEEPLINK DEBUG: API 에러 코드 확인
|
||||
if (
|
||||
res?.data?.retCode === 602 ||
|
||||
res?.data?.retCode === 603 ||
|
||||
res?.data?.retCode === 604
|
||||
) {
|
||||
console.log("🔍 [DEEPLINK DEBUG] ===== API 에러 발생 =====");
|
||||
console.log("🔍 [DEEPLINK DEBUG] 에러 코드:", res?.data?.retCode);
|
||||
console.log("🔍 [DEEPLINK DEBUG] API URL:", url);
|
||||
console.log("🔍 [DEEPLINK DEBUG] 응답 데이터:", res?.data);
|
||||
}
|
||||
|
||||
// 602 요청 국가 불일치
|
||||
if (res?.data?.retCode === 602) {
|
||||
console.log(
|
||||
"🔍 [DEEPLINK DEBUG] ❌ 602 에러: 요청 국가 불일치 - 국가 변경 팝업 표시"
|
||||
);
|
||||
dispatch(
|
||||
setShowPopup(ACTIVE_POPUP.changeCountyPopup, {
|
||||
data: res.data.retCode,
|
||||
@@ -227,9 +218,6 @@ export const TAxios = (
|
||||
}
|
||||
// 603 서비스 국가 아님
|
||||
if (res?.data?.retCode === 603) {
|
||||
console.log(
|
||||
"🔍 [DEEPLINK DEBUG] ❌ 603 에러: 서비스 지원 안하는 국가 - 지원 안함 팝업 표시"
|
||||
);
|
||||
dispatch(
|
||||
setShowPopup(ACTIVE_POPUP.unSupportedCountryPopup, {
|
||||
data: res.data.retCode,
|
||||
@@ -239,9 +227,6 @@ export const TAxios = (
|
||||
}
|
||||
|
||||
if (res?.data?.retCode === 604) {
|
||||
console.log(
|
||||
"🔍 [DEEPLINK DEBUG] ❌ 604 에러: 서비스 지원 안하는 언어"
|
||||
);
|
||||
//todo "NotServiceLanguage"
|
||||
return;
|
||||
}
|
||||
@@ -291,7 +276,7 @@ export const TAxiosPromise = (
|
||||
success: true,
|
||||
data: response.data,
|
||||
response: response,
|
||||
error: null,
|
||||
error: null
|
||||
});
|
||||
},
|
||||
// onFail - 에러도 resolve로 처리하여 throw 방지
|
||||
@@ -301,7 +286,7 @@ export const TAxiosPromise = (
|
||||
success: false,
|
||||
data: null,
|
||||
response: null,
|
||||
error: error,
|
||||
error: error
|
||||
});
|
||||
},
|
||||
noTokenRefresh
|
||||
@@ -333,14 +318,10 @@ export const TAxiosAdvancedPromise = (
|
||||
|
||||
const attemptRequest = () => {
|
||||
attempts++;
|
||||
console.log(
|
||||
`TAxiosPromise attempt ${attempts}/${maxAttempts} for ${baseUrl}`
|
||||
);
|
||||
console.log(`TAxiosPromise attempt ${attempts}/${maxAttempts} for ${baseUrl}`);
|
||||
|
||||
const timeoutId = setTimeout(() => {
|
||||
const timeoutError = new Error(
|
||||
`Request timeout after ${timeout}ms for ${baseUrl}`
|
||||
);
|
||||
const timeoutError = new Error(`Request timeout after ${timeout}ms for ${baseUrl}`);
|
||||
if (throwOnError) {
|
||||
reject(timeoutError);
|
||||
} else {
|
||||
@@ -348,7 +329,7 @@ export const TAxiosAdvancedPromise = (
|
||||
success: false,
|
||||
data: null,
|
||||
response: null,
|
||||
error: timeoutError,
|
||||
error: timeoutError
|
||||
});
|
||||
}
|
||||
}, timeout);
|
||||
@@ -363,29 +344,22 @@ export const TAxiosAdvancedPromise = (
|
||||
// onSuccess
|
||||
(response) => {
|
||||
clearTimeout(timeoutId);
|
||||
console.log(
|
||||
`TAxiosPromise success on attempt ${attempts} for ${baseUrl}`
|
||||
);
|
||||
console.log(`TAxiosPromise success on attempt ${attempts} for ${baseUrl}`);
|
||||
resolve({
|
||||
success: true,
|
||||
data: response.data,
|
||||
response: response,
|
||||
error: null,
|
||||
error: null
|
||||
});
|
||||
},
|
||||
// onFail
|
||||
(error) => {
|
||||
clearTimeout(timeoutId);
|
||||
console.error(
|
||||
`TAxiosPromise error on attempt ${attempts} for ${baseUrl}:`,
|
||||
error
|
||||
);
|
||||
|
||||
console.error(`TAxiosPromise error on attempt ${attempts} for ${baseUrl}:`, error);
|
||||
|
||||
// 재시도 로직
|
||||
if (attempts < maxAttempts) {
|
||||
console.log(
|
||||
`Retrying in ${retryDelay}ms... (${attempts}/${maxAttempts})`
|
||||
);
|
||||
console.log(`Retrying in ${retryDelay}ms... (${attempts}/${maxAttempts})`);
|
||||
setTimeout(() => {
|
||||
attemptRequest();
|
||||
}, retryDelay);
|
||||
@@ -398,7 +372,7 @@ export const TAxiosAdvancedPromise = (
|
||||
success: false,
|
||||
data: null,
|
||||
response: null,
|
||||
error: error,
|
||||
error: error
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -412,51 +386,23 @@ export const TAxiosAdvancedPromise = (
|
||||
};
|
||||
|
||||
// HTTP 메소드별 편의 함수들 (안전한 버전)
|
||||
export const TAxiosGet = async (
|
||||
dispatch,
|
||||
getState,
|
||||
baseUrl,
|
||||
urlParams = {},
|
||||
options = {}
|
||||
) => {
|
||||
return await TAxiosPromise(
|
||||
dispatch,
|
||||
getState,
|
||||
"get",
|
||||
baseUrl,
|
||||
urlParams,
|
||||
{},
|
||||
options.noTokenRefresh
|
||||
);
|
||||
export const TAxiosGet = async (dispatch, getState, baseUrl, urlParams = {}, options = {}) => {
|
||||
return await TAxiosPromise(dispatch, getState, 'get', baseUrl, urlParams, {}, options.noTokenRefresh);
|
||||
};
|
||||
|
||||
export const TAxiosPost = async (
|
||||
dispatch,
|
||||
getState,
|
||||
baseUrl,
|
||||
params = {},
|
||||
options = {}
|
||||
) => {
|
||||
return await TAxiosPromise(
|
||||
dispatch,
|
||||
getState,
|
||||
"post",
|
||||
baseUrl,
|
||||
{},
|
||||
params,
|
||||
options.noTokenRefresh
|
||||
);
|
||||
export const TAxiosPost = async (dispatch, getState, baseUrl, params = {}, options = {}) => {
|
||||
return await TAxiosPromise(dispatch, getState, 'post', baseUrl, {}, params, options.noTokenRefresh);
|
||||
};
|
||||
|
||||
// 안전한 다중 요청 처리
|
||||
export const TAxiosAll = async (requests) => {
|
||||
try {
|
||||
const results = await Promise.all(requests);
|
||||
|
||||
|
||||
// 모든 결과를 안전하게 처리
|
||||
const successResults = [];
|
||||
const failedResults = [];
|
||||
|
||||
|
||||
results.forEach((result, index) => {
|
||||
if (result.success) {
|
||||
successResults.push({ index, result: result.data });
|
||||
@@ -464,20 +410,20 @@ export const TAxiosAll = async (requests) => {
|
||||
failedResults.push({ index, error: result.error });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return {
|
||||
success: failedResults.length === 0,
|
||||
successResults,
|
||||
failedResults,
|
||||
allResults: results,
|
||||
allResults: results
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("TAxiosAll unexpected error:", error);
|
||||
console.error('TAxiosAll unexpected error:', error);
|
||||
return {
|
||||
success: false,
|
||||
successResults: [],
|
||||
failedResults: [{ index: -1, error }],
|
||||
allResults: [],
|
||||
allResults: []
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -486,7 +432,7 @@ export const TAxiosAll = async (requests) => {
|
||||
export const TAxiosSequential = async (requests) => {
|
||||
const results = [];
|
||||
const errors = [];
|
||||
|
||||
|
||||
for (let i = 0; i < requests.length; i++) {
|
||||
try {
|
||||
const result = await requests[i];
|
||||
@@ -498,37 +444,33 @@ export const TAxiosSequential = async (requests) => {
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push({ index: i, error });
|
||||
console.error(
|
||||
`TAxiosSequential unexpected error at request ${i}:`,
|
||||
error
|
||||
);
|
||||
console.error(`TAxiosSequential unexpected error at request ${i}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
success: errors.length === 0,
|
||||
results,
|
||||
errors,
|
||||
errors
|
||||
};
|
||||
};
|
||||
|
||||
// 안전한 Redux Thunk 헬퍼
|
||||
export const createSafeApiThunk = (apiCall) => {
|
||||
return (...args) =>
|
||||
async (dispatch, getState) => {
|
||||
try {
|
||||
const result = await apiCall(dispatch, getState, ...args);
|
||||
return result; // 이미 안전한 형태로 반환됨
|
||||
} catch (error) {
|
||||
console.error("API thunk unexpected error:", error);
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
response: null,
|
||||
error,
|
||||
};
|
||||
}
|
||||
};
|
||||
return (...args) => async (dispatch, getState) => {
|
||||
try {
|
||||
const result = await apiCall(dispatch, getState, ...args);
|
||||
return result; // 이미 안전한 형태로 반환됨
|
||||
} catch (error) {
|
||||
console.error('API thunk unexpected error:', error);
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
response: null,
|
||||
error
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// 실제 사용 예시들 (안전한 버전)
|
||||
@@ -538,16 +480,16 @@ export const safeUsageExamples = {
|
||||
const result = await TAxiosPromise(
|
||||
dispatch,
|
||||
getState,
|
||||
"get",
|
||||
'get',
|
||||
URLS.GET_HOME_TERMS,
|
||||
{ trmsTpCdList: "MST00401, MST00402", mbrNo: "12345" }
|
||||
);
|
||||
|
||||
|
||||
if (result.success) {
|
||||
console.log("Success:", result.data);
|
||||
console.log('Success:', result.data);
|
||||
return result.data;
|
||||
} else {
|
||||
console.error("API call failed:", result.error);
|
||||
console.error('API call failed:', result.error);
|
||||
// 에러를 throw하지 않고 기본값 반환하거나 사용자에게 안내
|
||||
return null;
|
||||
}
|
||||
@@ -555,24 +497,26 @@ export const safeUsageExamples = {
|
||||
|
||||
// 2. retCode 체크를 포함한 안전한 처리
|
||||
safeWithRetCodeCheck: async (dispatch, getState) => {
|
||||
const result = await TAxiosGet(dispatch, getState, URLS.GET_HOME_TERMS, {
|
||||
trmsTpCdList: "MST00401, MST00402",
|
||||
mbrNo: "12345",
|
||||
});
|
||||
|
||||
const result = await TAxiosGet(
|
||||
dispatch,
|
||||
getState,
|
||||
URLS.GET_HOME_TERMS,
|
||||
{ trmsTpCdList: "MST00401, MST00402", mbrNo: "12345" }
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
console.error("Network error:", result.error);
|
||||
return { success: false, message: "네트워크 오류가 발생했습니다." };
|
||||
console.error('Network error:', result.error);
|
||||
return { success: false, message: '네트워크 오류가 발생했습니다.' };
|
||||
}
|
||||
|
||||
|
||||
if (result.data.retCode !== 0) {
|
||||
console.error("API error:", result.data.retCode, result.data.retMsg);
|
||||
return {
|
||||
success: false,
|
||||
message: result.data.retMsg || "API 오류가 발생했습니다.",
|
||||
console.error('API error:', result.data.retCode, result.data.retMsg);
|
||||
return {
|
||||
success: false,
|
||||
message: result.data.retMsg || 'API 오류가 발생했습니다.'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return { success: true, data: result.data };
|
||||
},
|
||||
|
||||
@@ -581,41 +525,40 @@ export const safeUsageExamples = {
|
||||
const requests = [
|
||||
TAxiosGet(dispatch, getState, URLS.GET_HOME_TERMS, { mbrNo: "12345" }),
|
||||
TAxiosGet(dispatch, getState, URLS.GET_USER_INFO, { mbrNo: "12345" }),
|
||||
TAxiosPost(dispatch, getState, URLS.UPDATE_SETTINGS, {
|
||||
setting: "value",
|
||||
}),
|
||||
TAxiosPost(dispatch, getState, URLS.UPDATE_SETTINGS, { setting: "value" })
|
||||
];
|
||||
|
||||
|
||||
const result = await TAxiosAll(requests);
|
||||
|
||||
|
||||
if (result.success) {
|
||||
console.log("All requests succeeded");
|
||||
return result.successResults.map((item) => item.result);
|
||||
console.log('All requests succeeded');
|
||||
return result.successResults.map(item => item.result);
|
||||
} else {
|
||||
console.error("Some requests failed:", result.failedResults);
|
||||
console.error('Some requests failed:', result.failedResults);
|
||||
// 부분적 성공도 처리 가능
|
||||
return {
|
||||
successData: result.successResults.map((item) => item.result),
|
||||
errors: result.failedResults,
|
||||
successData: result.successResults.map(item => item.result),
|
||||
errors: result.failedResults
|
||||
};
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 컴포넌트에서의 안전한 사용법
|
||||
export const ComponentUsageExample = () => {
|
||||
const dispatch = useDispatch();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
|
||||
const handleFetchTerms = async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
|
||||
const result = await dispatch(fetchCurrentUserHomeTermsSafe());
|
||||
|
||||
|
||||
setLoading(false);
|
||||
|
||||
|
||||
if (result.success) {
|
||||
console.log("Terms fetched successfully");
|
||||
// 성공 처리 (예: 성공 토스트 표시)
|
||||
@@ -625,13 +568,13 @@ export const ComponentUsageExample = () => {
|
||||
// 에러 처리 (예: 에러 토스트 표시)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button onClick={handleFetchTerms} disabled={loading}>
|
||||
{loading ? "로딩 중..." : "약관 정보 가져오기"}
|
||||
{loading ? '로딩 중...' : '약관 정보 가져오기'}
|
||||
</button>
|
||||
{error && <div className="error-message">{error}</div>}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user