์ฃผ์š” ์ž‘์—…

  • ์—๋Ÿฌ์ฒ˜๋ฆฌ ๋ณด์™„ โœ… 2024-09-12
    • ์„œ๋ฒ„
      • ์š”์ฒญ ๊ฐ์ฒด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ Validation Error ์ถ”๊ฐ€
      • ์—๋Ÿฌ๋ณ„๋กœ ๋ช…์‹œ ๋ช…ํ™•ํ™”
    • ํด๋ผ์ด์–ธํŠธ
      • ์—๋Ÿฌํ™”๋ฉด ๋„์šฐ๊ธฐ
        • ๊ฐ ์ฝ”๋“œ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฉ”์„ธ์ง€ ๋„์šฐ๊ธฐ
  • ์ปฌ๋Ÿผ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ
    • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ถ”๊ฐ€

ํ•™์Šต ํ‚ค์›Œ๋“œ

๊ณ ๋ฏผ ๋ฐ ํ•ด๊ฒฐ๊ณผ์ •

ํด๋ผ์ด์–ธํŠธ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ๋ฌธ์ œ

ํด๋ผ์ด์–ธํŠธ์˜ ์˜ค๋ฅ˜์˜ ๊ฒฝ์šฐ ๊ฐ๊ฐ์˜ ์˜ค๋ฅ˜์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ๊ณ ๋ฏผ์ด ์žˆ์—ˆ๋‹ค.

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์— ๋„˜๊ธฐ๊ฒŒ ๋˜๋ฉด, ํ•ด๋‹น ๋ฉ”์‹œ์ง€๋กœ ๋œ ๋ชจ๋‹ฌ์„ ๋ Œ๋”๋ง์‹œํ‚ค๊ณ  ์ด์— ๋งž๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋กํ•ด์ฃผ๋Š” ๋กœ์ง์„ ํ•จ์ˆ˜ํ˜•์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค.