-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FE] HTTP 요청 오류 시 status code별 오류 상황을 대응하는 함수 생성 #151
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { API_ERROR_MESSAGE, SERVER_ERROR_REGEX } from '@/constants'; | ||
|
||
const createApiErrorMessage = (statusCode: number) => { | ||
const isServerError = SERVER_ERROR_REGEX.test(statusCode.toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아래 코드처럼 서버 에러를 확인하는 방법은 여러가지가 있었을 거라 생각하는데, 정규표현식을 사용한 이유가 있을까요? const isServerError = statusCode >= 500 && statusCode < 600; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 500, 600이라는 매직넘버가 생기고 매직넘버를 피하기 위해서 상수처리는 해야해요. |
||
|
||
if (isServerError) return API_ERROR_MESSAGE.serverError; | ||
|
||
if (statusCode in API_ERROR_MESSAGE) return API_ERROR_MESSAGE[statusCode]; | ||
}; | ||
|
||
export default createApiErrorMessage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
interface ApiErrorMessages { | ||
[key: number]: string; | ||
serverError: string; | ||
} | ||
|
||
export const API_ERROR_MESSAGE: ApiErrorMessages = { | ||
400: '유효하지 않은 요청 형식이에요.', | ||
401: '인증을 실패했어요.', | ||
403: '요청권한이 없어요.', | ||
404: '요청한 페이지를 찾을 수 없어요.', | ||
422: '요청이 잘못되었거나 비즈니스 로직을 위반했어요.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 422 에러는 요청이 제대로 갔지만 처리되지 않는 오류로 알고 있어요! https://developer.mozilla.org/ko/docs/Web/HTTP/Status/422 오류 메시지를 다시 고민해보면 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
클라이언트의 json이 잘못된 경우인 400과 보낸 요청의 유효성 검사가 실패한 422의 에러 메세지가 구별이 될 수 있도록 |
||
serverError: '서버 오류가 발생했어요.', | ||
}; | ||
|
||
export const SERVER_ERROR_REGEX = /^5\d{2}$/; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './page'; | ||
export * from './apiErrorMessage'; | ||
export * from './review'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
서버 에러인지를 판별할 때 특별히 정규 표현식을 사용한 이유가 있을까요?
그리고 코드 보고 갑자기 생각난 건데 문자열로 변환이 필요할 때
toString
과String
중 무엇을 사용할지 팀원들과 간단하게 논의해보면 좋겠어요~There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
500대의 에러를 잡기 위해서 정규표현식을 사용했어요.