idea: apply common error message#62
Draft
yabsed wants to merge 1 commit into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 제목
v2 API 에러 응답 공통 핸들러 도입
PR 본문
요약
v2 API에 한해 에러 응답을 공통 JSON envelope로 표준화했습니다.
기존에는 각 API route가 직접
try/catch를 두고ZodError,BadRequestError,UserNotFoundError, unknown error 등을 개별 처리했습니다. 그래서 같은 status라도 route마다 body 형태와 메시지 대소문자가 달랐습니다.이번 변경에서는 v2 route의 에러 처리 책임을
withV2ApiHandler로 옮겼습니다.{ "error": { "code": "VALIDATION_ERROR", "message": "요청 값이 올바르지 않습니다.", "details": [], "requestId": "..." } }핵심 원리
route handler는 이제 성공 흐름만 표현합니다.
예를 들어
clubs/register는 다음 일만 합니다.짧아진 이유는
try/catch가 사라졌기 때문입니다. 사라진 게 아니라 아래 래퍼로 이동했습니다.withV2ApiHandler가 하는 일withV2ApiHandler는 v2 API route의 공통 입구입니다.405 METHOD_NOT_ALLOWED를 반환합니다.handler(req, res)를 실행합니다.mapError가 있으면 먼저 적용합니다.{ error: { code, message, details, requestId } }형태로 응답합니다.공통 매핑 예시는 다음과 같습니다.
z.ZodError→400 VALIDATION_ERRORBadRequestError→400 BAD_REQUEST또는 구체 codeUnauthorizedError→401 UNAUTHORIZEDForbiddenError→403 FORBIDDENUserNotFoundError→ 기본404 USER_NOT_FOUNDNotFoundError→404 NOT_FOUND또는 구체 codeConflictError→409 CONFLICT또는 구체 code500 INTERNAL_SERVER_ERRORroute별 예외는
mapError로 표현clubs/register에서는UserNotFoundError를 기본값인404가 아니라401로 내려야 합니다. 이런 route별 의미 차이는mapError에만 남깁니다.즉, 대부분의 에러는 공통 규칙을 따르고, 정말 route 의미가 다른 경우만 local mapper로 덮어씁니다.
v2 전용 범위
이번 변경은 v2 API 표준화를 위한 작업입니다.
withV2ApiHandler를 사용합니다./api/v2/요청에만 새 error envelope를 반환합니다.unauthorized응답을 유지합니다.적용된 대표 route
pages/api/v2/clubs/[uuid]/index.tspages/api/v2/clubs/search/index.tspages/api/v2/clubs/register/index.tspages/api/v2/managers/me/clubs/[uuid]/index.tspages/api/v2/terms/agree.tspages/api/v2/announcements/dismiss.tspages/api/v2/users/me/recent-searches/index.tspages/api/v2/users/me/voices/index.ts검증
tsc --noEmiteslintprettier