lemonpie313 / spartaNodejs06-recruitment

0 stars 0 forks source link

[1] 3-Layered Architecture 적용 리팩토링 (2) Layer 분리 후 에러 처리 #2

Closed lemonpie313 closed 1 month ago

lemonpie313 commented 1 month ago
lemonpie313 commented 1 month ago

Http Error Class 에러 처리 Middleware에서 사용하려면 코드 수정 필요

// src/errors/http.error.js

import { HTTP_STATUS } from '../constants/http-status.constant.js';

class BadRequest {
  constructor(message = BadRequest.name) {
    this.message = message;
    this.status = HTTP_STATUS.BAD_REQUEST;
  }
}

class Unauthorized {
  constructor(message = Unauthorized.name) {
    this.message = message;
    this.status = HTTP_STATUS.UNAUTHORIZED;
  }
}

class Forbidden {
  constructor(message = Forbidden.name) {
    this.message = message;
    this.status = HTTP_STATUS.FORBIDDEN;
  }
}

class NotFound {
  constructor(message = NotFound.name) {
    this.message = message;
    this.status = HTTP_STATUS.NOT_FOUND;
  }
}

class Conflict {
  constructor(message = Conflict.name) {
    this.message = message;
    this.status = HTTP_STATUS.CONFLICT;
  }
}

class InternalServerError {
  constructor(message = InternalServerError.name) {
    this.message = message;
    this.status = HTTP_STATUS.INTERNAL_SERVER_ERROR;
  }
}

export const HttpError = {
  BadRequest,
  Unauthorized,
  Forbidden,
  NotFound,
  Conflict,
  InternalServerError,
};
// src/constants/http-status.constant.js

export const HTTP_STATUS = {
  OK: 200, // 호출에 성공했을 때
  CREATED: 201, // 생성에 성공했을 때
  BAD_REQUEST: 400, // 사용자가 잘못 했을 때 (예: 입력 값을 빠뜨렸을 때)
  UNAUTHORIZED: 401, // 인증 실패 unauthenciated (예: 비밀번호가 틀렸을 때)
  FORBIDDEN: 403, // 인가 실패 unauthorized (예: 접근 권한이 없을 때)
  NOT_FOUND: 404, // 데이터가 없는 경우
  CONFLICT: 409, // 충돌이 발생했을 때 (예: 이메일 중복)
  INTERNAL_SERVER_ERROR: 500, // 예상치 못한 에러가 발생했을 때
};