-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor: API 에러 처리 공통 핸들러 도입 및 onError 정책 통일 #202
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { HTTPError } from 'ky'; | ||
|
|
||
| export interface ToastApi { | ||
| attention: (message: string) => void; | ||
| } | ||
|
|
||
| export interface HandleApiErrorOptions { | ||
| /** 사용자에게 노출할 기본 실패 문구 (서버 메시지가 없거나 사용 불가일 때 사용) */ | ||
| fallback: string; | ||
| /** 토스트 인스턴스 (useToast() 결과) */ | ||
| toast: ToastApi; | ||
| /** 디버깅용 컨텍스트 (예: 'expense.update') */ | ||
| context?: string; | ||
| /** | ||
| * true이면 HTTP 응답에서 받은 서버 메시지(error.message)를 우선 노출. | ||
| * 네트워크 오류 등 응답이 없는 경우엔 fallback을 사용. | ||
| * 기본 false. | ||
| */ | ||
| preferServerMessage?: boolean; | ||
| } | ||
|
|
||
| const SILENT_STATUS = new Set([401, 403]); | ||
|
|
||
| /** | ||
| * mutation/쿼리의 onError에서 사용할 공통 핸들러. | ||
| * - 401/403은 client.ts에서 reissue/리다이렉트 처리하므로 토스트를 띄우지 않음 | ||
| * - 기본은 fallback 문구 노출. preferServerMessage=true이면 HTTPError의 서버 메시지 우선 | ||
| * - console.error를 일관 포맷으로 출력 | ||
| */ | ||
| export const handleApiError = (error: unknown, options: HandleApiErrorOptions) => { | ||
| const { fallback, toast, context, preferServerMessage = false } = options; | ||
|
|
||
| if (error instanceof HTTPError && SILENT_STATUS.has(error.response.status)) { | ||
| return; | ||
| } | ||
|
|
||
| console.error(`[API Error]${context ? ` ${context}` : ''}:`, error); | ||
|
|
||
| const serverMessage = | ||
| preferServerMessage && error instanceof HTTPError && error.message ? error.message : null; | ||
|
|
||
| toast.attention(serverMessage ?? fallback); | ||
| }; | ||
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
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
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
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
Oops, something went wrong.
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.
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.
preferServerMessage사용 시 ky의 기본 에러 메시지가 노출될 위험client.ts의beforeError는 응답 body를 JSON 파싱해body.message가 있을 때만error.message를 덮어씁니다. 따라서 응답이 JSON이 아니거나message필드가 없는 경우error.message는 ky의 기본 문자열(예:Request failed with status code 500)이 그대로 남습니다. 현재 로직은error.message가 truthy이기만 하면 이를 토스트에 노출하므로,preferServerMessage: true인useWithdraw같은 호출에서 사용자에게 영문 ky 메시지가 그대로 보일 수 있습니다.서버가 실제로 메시지를 내려줬을 때만 이를 우선하도록, 서버 메시지를 별도 필드에 저장해 구분하는 것을 권장합니다.
♻️ 제안: 서버 메시지를 별도 필드로 보관
// apps/web/src/shared/api/client.ts (beforeError) if (error.response) { try { const body = (await error.response.clone().json()) as { message?: string }; if (body?.message) { error.message = body.message; + (error as HTTPError & { serverMessage?: string }).serverMessage = body.message; } } catch { // JSON 파싱 실패 시 원본 message 유지 } }📝 Committable suggestion
🤖 Prompt for AI Agents