Skip to content

Commit 2159da3

Browse files
committed
feat: enhance error handling in API requests
- Improve error handling in commonAPIPost and commonAPIPostWithoutRetry functions to provide more specific alerts for various HTTP status codes (404, 401, 403, 500). - Update getMciList, getMci, and getMciVm functions to return empty arrays or null for 404 errors, ensuring smoother user experience when no data is found. - Add network error handling to alert users about connection issues.
1 parent 46a1995 commit 2159da3

2 files changed

Lines changed: 155 additions & 36 deletions

File tree

front/assets/js/common/api/http.js

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,64 @@ export async function commonAPIPost(url, data, attempt) {
3030
alert("too many request : "+ error.message);
3131
return error
3232
}
33+
// 404 에러는 데이터가 없는 정상적인 상황이므로 토큰 갱신하지 않음
34+
if (error.response && error.response.status === 404) {
35+
console.log("Resource not found (404) - this may be normal for empty data");
36+
return error;
37+
}
38+
// 401 Unauthorized는 토큰 만료 또는 인증 실패
39+
if (error.response && error.response.status === 401) {
40+
const authrefreshStatus = await webconsolejs["common/cookie/authcookie"].refreshCookieAccessToken();
41+
if (authrefreshStatus) {
42+
console.log("refreshCookieAccessToken success. Retrying request with refreshed token...");
43+
return commonAPIPost(url, data, true);
44+
} else {
45+
alert("Session has expired. Please login again.");
46+
window.location = "/auth/login"
47+
return
48+
}
49+
}
50+
// 403 Forbidden은 권한 부족
51+
if (error.response && error.response.status === 403) {
52+
alert("Insufficient permissions. Please contact your administrator.");
53+
return error;
54+
}
55+
// 500 Internal Server Error는 서버 오류
56+
if (error.response && error.response.status === 500) {
57+
alert("Server error occurred. Please try again later.");
58+
return error;
59+
}
60+
// 기타 HTTP 에러
3361
if (error.response && (error.response.status !== 200)){
3462
const authrefreshStatus = await webconsolejs["common/cookie/authcookie"].refreshCookieAccessToken();
3563
if (authrefreshStatus) {
3664
console.log("refreshCookieAccessToken success. Retrying request with refreshed token...");
3765
return commonAPIPost(url, data, true);
3866
} else {
39-
alert("session is expired");
67+
// 토큰 갱신 실패 시에도 세션 만료로 간주
68+
alert("Session has expired. Please login again.");
4069
window.location = "/auth/login"
4170
return
4271
}
4372
}
4473
}
4574
deactivePageLoader()
46-
alert("request fail : "+ error.message);
75+
76+
// 네트워크 오류나 기타 예외 상황 처리
77+
if (!error.response) {
78+
// 네트워크 오류 (서버에 연결할 수 없음)
79+
if (error.code === 'ECONNABORTED') {
80+
alert("Request timeout. Please check your network connection and try again.");
81+
} else if (error.code === 'ERR_NETWORK') {
82+
alert("Network connection failed. Please check your internet connection and try again.");
83+
} else {
84+
alert("An error occurred while processing the request: " + error.message);
85+
}
86+
} else {
87+
// HTTP 에러가 있지만 위에서 처리되지 않은 경우
88+
alert("An error occurred while processing the request. (Status code: " + error.response.status + ")");
89+
}
90+
4791
return error
4892
}
4993
}
@@ -69,6 +113,31 @@ export async function commonAPIPostWithoutRetry(url, data) {
69113
console.log("Error: ", error.response ? error.response.status : error.message);
70114
console.log("----------------------------");
71115
console.log("Request failed :", error);
116+
117+
// 에러 메시지 표시 (retry 없이)
118+
if (error.response) {
119+
if (error.response.status === 401) {
120+
alert("Authentication required. Please login again.");
121+
} else if (error.response.status === 403) {
122+
alert("Insufficient permissions. Please contact your administrator.");
123+
} else if (error.response.status === 404) {
124+
console.log("Requested resource not found.");
125+
} else if (error.response.status === 500) {
126+
alert("Server error occurred. Please try again later.");
127+
} else {
128+
alert("An error occurred while processing the request. (Status code: " + error.response.status + ")");
129+
}
130+
} else {
131+
// 네트워크 오류
132+
if (error.code === 'ECONNABORTED') {
133+
alert("Request timeout. Please check your network connection and try again.");
134+
} else if (error.code === 'ERR_NETWORK') {
135+
alert("Network connection failed. Please check your internet connection and try again.");
136+
} else {
137+
alert("An error occurred while processing the request: " + error.message);
138+
}
139+
}
140+
72141
return error
73142
}
74143
}

front/assets/js/common/api/services/mci_api.js

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export async function getMciList(nsId) {
66

77
if (nsId == "") {
88
console.log("Project has not set")
9-
return;
9+
return [];
1010
}
1111

1212
var data = {
@@ -16,14 +16,26 @@ export async function getMciList(nsId) {
1616
};
1717

1818
var controller = "/api/" + "mc-infra-manager/" + "GetAllMci";
19-
const response = await webconsolejs["common/api/http"].commonAPIPost(
20-
controller,
21-
data
22-
)
23-
24-
var mciList = response.data.responseData;
25-
26-
return mciList
19+
20+
try {
21+
const response = await webconsolejs["common/api/http"].commonAPIPost(
22+
controller,
23+
data
24+
)
25+
26+
var mciList = response.data.responseData;
27+
return mciList || [];
28+
} catch (error) {
29+
// 404 에러 (데이터가 없는 경우)는 정상적인 상황이므로 빈 배열 반환
30+
if (error.response && error.response.status === 404) {
31+
console.log("No MCI data found for namespace:", nsId);
32+
return [];
33+
}
34+
35+
// 다른 에러는 그대로 throw
36+
console.error("Error fetching MCI list:", error);
37+
throw error;
38+
}
2739
}
2840

2941
// 받아온 project(namespace)로 MciList Id Arr GET
@@ -58,7 +70,7 @@ export async function getMciList(nsId) {
5870
export async function getMci(nsId, mciId) {
5971
if (nsId == "" || nsId == undefined || mciId == undefined || mciId == "") {
6072
console.log(" undefined nsId: " + nsId + " mciId " + mciId);
61-
return;
73+
return null;
6274
}
6375
const data = {
6476
pathParams: {
@@ -68,21 +80,34 @@ export async function getMci(nsId, mciId) {
6880
}
6981

7082
var controller = "/api/" + "mc-infra-manager/" + "GetMci";
71-
const response = await webconsolejs["common/api/http"].commonAPIPost(
72-
controller,
73-
data
74-
);
75-
76-
// error check를 위해 response를 return
77-
return response.data
83+
84+
try {
85+
const response = await webconsolejs["common/api/http"].commonAPIPost(
86+
controller,
87+
data
88+
);
89+
90+
// error check를 위해 response를 return
91+
return response.data;
92+
} catch (error) {
93+
// 404 에러 (MCI가 없는 경우)는 정상적인 상황이므로 null 반환
94+
if (error.response && error.response.status === 404) {
95+
console.log("MCI not found:", mciId, "in namespace:", nsId);
96+
return null;
97+
}
98+
99+
// 다른 에러는 그대로 throw
100+
console.error("Error fetching MCI:", error);
101+
throw error;
102+
}
78103
}
79104

80105

81106
// mci vm 단건 조회
82107
export async function getMciVm(nsId, mciId, vmId) {
83108
if (nsId == "" || nsId == undefined || mciId == undefined || vmId == "" || vmId == undefined || vmId == "") {
84109
console.log(" undefined nsId: " + nsId, + " mciId " + mciId, ", vmId " + vmId);
85-
return;
110+
return null;
86111
}
87112
const data = {
88113
pathParams: {
@@ -93,13 +118,26 @@ export async function getMciVm(nsId, mciId, vmId) {
93118
}
94119

95120
var controller = "/api/" + "mc-infra-manager/" + "GetMciVm";
96-
const response = await webconsolejs["common/api/http"].commonAPIPost(
97-
controller,
98-
data
99-
);
100-
101-
// error check를 위해 response를 return
102-
return response.data
121+
122+
try {
123+
const response = await webconsolejs["common/api/http"].commonAPIPost(
124+
controller,
125+
data
126+
);
127+
128+
// error check를 위해 response를 return
129+
return response.data;
130+
} catch (error) {
131+
// 404 에러 (VM이 없는 경우)는 정상적인 상황이므로 null 반환
132+
if (error.response && error.response.status === 404) {
133+
console.log("VM not found:", vmId, "in MCI:", mciId, "namespace:", nsId);
134+
return null;
135+
}
136+
137+
// 다른 에러는 그대로 throw
138+
console.error("Error fetching MCI VM:", error);
139+
throw error;
140+
}
103141
}
104142

105143
// mciLifeCycle 제어 option : reboot / suspend / resume / terminate
@@ -677,7 +715,7 @@ export async function getPolicyList(nsId) {
677715

678716
if (nsId == "") {
679717
console.log("Project has not set")
680-
return;
718+
return [];
681719
}
682720

683721
var data = {
@@ -687,14 +725,26 @@ export async function getPolicyList(nsId) {
687725
};
688726

689727
var controller = "/api/" + "mc-infra-manager/" + "Getallmcipolicy";
690-
const response = await webconsolejs["common/api/http"].commonAPIPost(
691-
controller,
692-
data
693-
)
694-
695-
var policyList = response.data.responseData;
696-
697-
return policyList
728+
729+
try {
730+
const response = await webconsolejs["common/api/http"].commonAPIPost(
731+
controller,
732+
data
733+
)
734+
735+
var policyList = response.data.responseData;
736+
return policyList || [];
737+
} catch (error) {
738+
// 404 에러 (정책이 없는 경우)는 정상적인 상황이므로 빈 배열 반환
739+
if (error.response && error.response.status === 404) {
740+
console.log("No policy data found for namespace:", nsId);
741+
return [];
742+
}
743+
744+
// 다른 에러는 그대로 throw
745+
console.error("Error fetching policy list:", error);
746+
throw error;
747+
}
698748
}
699749

700750
export async function deletePolicy(nsId, mciId) {

0 commit comments

Comments
 (0)