์ฃผ์ ์์
- ์๋ฌ์ฒ๋ฆฌ ๋ณด์ โ
2024-09-12
- ์๋ฒ
- ์์ฒญ ๊ฐ์ฒด ์ ํจ์ฑ ๊ฒ์ฌ Validation Error ์ถ๊ฐ
- ์๋ฌ๋ณ๋ก ๋ช ์ ๋ช ํํ
- ํด๋ผ์ด์ธํธ
- ์๋ฌํ๋ฉด ๋์ฐ๊ธฐ
- ๊ฐ ์ฝ๋์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ฉ์ธ์ง ๋์ฐ๊ธฐ
- ์๋ฌํ๋ฉด ๋์ฐ๊ธฐ
- ์๋ฒ
- ์ปฌ๋ผ ์ถ๊ฐ ๊ธฐ๋ฅ
- ํ ์คํธ ์ฝ๋ ์ถ๊ฐ
ํ์ต ํค์๋
- express async middleware
- FLUX ํจํด
- ์์ฒญ๊ณผ ์๋ต ๊ฐ์ฒด https://luxurious-share-af6.notion.site/4-aba822d7763544a5ad64778df911c630?pvs=4
๊ณ ๋ฏผ ๋ฐ ํด๊ฒฐ๊ณผ์
ํด๋ผ์ด์ธํธ ์ค๋ฅ ์ฒ๋ฆฌ ๋ฌธ์
ํด๋ผ์ด์ธํธ์ ์ค๋ฅ์ ๊ฒฝ์ฐ ๊ฐ๊ฐ์ ์ค๋ฅ์ ๋ํด ์ด๋ป๊ฒ ์ฒ๋ฆฌํด์ค์ผ ํ๋์ง์ ๋ํด ๊ณ ๋ฏผ์ด ์์๋ค.
http ๋ฉ์๋๋ฅผ ์คํํ๋ ๋ถ๋ถ์ apicall์ด๋ผ๋ ์ ํธ ํจ์๋ก ๋ง๋ค์์ง๋ง, ํด๋นํ๋ ํจ์๋ฅผ ์ด์ฉํ์ฌ api๋ฅผ ํธ์ถํ๋ ๋ถ๋ถ์ ๊ฒฝ์ฐ๋ฅผ try~catch๋ฌธ์ผ๋ก ์ก๋๋ค๋ฉด ํด๋น ์ ํธํจ์๋ http ์์ฒญ๊ณผ ์ฒ๋ฆฌ ๋ ๊ฐ์ง ์ญํ ์ ๋ชจ๋ ํ๋ค๋ ์ ์ด ๊ฑธ๋ฆฌ๊ธฐ๋ ํ๊ณ , http๋ฅผ ์์ฒญํ๊ณ ๋ฐ์ ์ค๋ฅ ์ค์์๋ 401 unauthorized๋ 400 Bad Request ๋ฑ 400๋ฒ๋์ ์๋ฌ๋ catch๋ฌธ์ผ๋ก ์ก์ง ๋ชปํ๋ค๋ ๋จ์ ์ด ์์๋ค.
import errorModal from "../errorModal/index.js";
function apiCall() {
async function get(uri, eventHandler) {
console.log("api_get", uri);
const response = await fetch(uri, {
method: "GET",
});
if (!response.ok) handleError(response.status);
const { data, path } = await response.json();
if (eventHandler) return eventHandler();
return { data, path };
}
async function post(uri, body, eventHandler) {
console.log("api_post", uri);
const response = await fetch(uri, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) handleError(response.status);
if (eventHandler) return eventHandler();
}
async function deleteMethod(uri, eventHandler) {
console.log("api_delete", uri);
const response = await fetch(uri, {
method: "DELETE",
});
if (!response.ok) handleError(response.status);
if (eventHandler) return eventHandler();
}
async function patch(uri, body, eventHandler) {
console.log("api_patch", uri);
const response = await fetch(uri, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) handleError(response.status);
const data = await response.json();
if (eventHandler) eventHandler();
return data;
}
function handleError(statcode) {
const status = {
400: "๋ด์ฉ์ ๋ค์ ํ์ธํด์ฃผ์ธ์!",
401: "์ฌ์ฉ์ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์์ด์ใ
.ใ
",
404: "์์ฒญํ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ ์ ์์ด์ใ
.ใ
",
500: "์๋ฒ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ด์ ใ
.ใ
",
default: "์์์น ๋ชปํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ด์ ใ
ใ
",
};
throw new Error(status[statcode]);
}
return {
post,
get,
deleteMethod,
patch,
};
}
export default apiCall();
์ด์ api๋ฅผ ์์ฒญํ๋ ํจ์์์ try~catch๋ฌธ์ ์ฌ์ฉํ๊ณ , api call์ ๊ฒฝ์ฐ๋ fetch๋ฅผ ํ ํ response.ok๋ฅผ ํตํด 200๋ฒ๋์ ์ํ ์ฝ๋๋ก ๋ฐํ๋๋ ๊ฒ๋ค๊ณผ 400๋ฒ๋์ ์ค๋ฅ๋ฅผ ๊ตฌ๋ถํ์ฌ 400๋ฒ๋ status code๋ฅผ ๋ฐ์์ ๊ฒฝ์ฐ์ error์ ์ ์ ํ ๋ฉ์์ง๋ฅผ ๋ฃ์ด ๋์ง์ผ๋ก์จ api ํธ์ถ ํจ์์ catch๋ฌธ์์ ์ก์ ์ ์๋๋ก ํ์๋ค.
import pug from "../utils/pugCompile.js";
import template from "./ui/index.js";
function errorModal(message) {
function onCickHandler() {
const button = document.querySelector(
".errorConfirmWrapper__button--conFirmButton"
);
button.addEventListener("click", () => {
document.querySelector(".errorModalBackground").remove();
});
}
function render(message) {
const component = pug.compiledToFunction(template);
const modalWrapper = document.createElement("div");
modalWrapper.innerHTML = component({ message: message });
document.body.appendChild(modalWrapper);
}
render(message);
onCickHandler();
return {
render,
};
}
export default errorModal;์๋ฌ์์๋ ๋ฉ์ธ์ง๋ฅผ catch๋ฌธ์์ error์ ๋ฉ์ธ์ง๋ฅผ errorModal์ ๋๊ธฐ๊ฒ ๋๋ฉด, ํด๋น ๋ฉ์์ง๋ก ๋ ๋ชจ๋ฌ์ ๋ ๋๋ง์ํค๊ณ ์ด์ ๋ง๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ๋ฑ๋กํด์ฃผ๋ ๋ก์ง์ ํจ์ํ์ผ๋ก ์คํ๋ ์ ์๋๋ก ํ์๋ค.