Repository navigation
fix: Sentry 에러 수집 중복과 누락 정리 #763
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
08d8111
fix: API 에러 Sentry 수집 창구를 인터셉터로 단일화
kanghaeun 09afce0
fix: Sentry API 에러 이슈를 엔드포인트·status 별로 분리
kanghaeun 41f1d34
docs: 에러 처리 정책에 Sentry 수집 기준 반영
kanghaeun 925638d
chore: Sentry 일간 요약에 앱 프로젝트 추가
kanghaeun 1293e1f
fix: FCM 토큰 등록 실패의 API 에러 중복 수집 제거
kanghaeun 7f80152
fix: 서버에서 난 API 에러의 Sentry 중복 수집 제거
kanghaeun 03923d2
fix: 서버 네트워크 오류·토큰 갱신 실패의 Sentry 수집 누락 수정
kanghaeun b806835
Merge branch 'dev' into fix/762-sentry-error-capture
kanghaeun 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
| import { isAxiosError } from 'axios'; | ||
|
|
||
| export async function register() { | ||
| if (process.env.NEXT_RUNTIME === 'nodejs') await import('./sentry.server.config'); | ||
| if (process.env.NEXT_RUNTIME === 'edge') await import('./sentry.edge.config'); | ||
| } | ||
|
|
||
| export const onRequestError = Sentry.captureRequestError; | ||
| export const onRequestError: typeof Sentry.captureRequestError = (error, request, errorContext) => { | ||
| if (isAxiosError(error)) return; | ||
| Sentry.captureRequestError(error, request, errorContext); | ||
| }; |
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,61 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
| import type { AxiosResponse, InternalAxiosRequestConfig } from 'axios'; | ||
| import { AxiosError } from 'axios'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import type { ApiErrorResponseT } from '@/types/api'; | ||
|
|
||
| import { captureApiError } from './captureError'; | ||
|
|
||
| vi.mock('@sentry/nextjs', () => ({ captureException: vi.fn() })); | ||
|
|
||
| const config = { method: 'post', url: '/api/v1/wishes/123?x=1' } as InternalAxiosRequestConfig; | ||
|
|
||
| const axiosError = ({ code, status }: { code?: string; status?: number }) => { | ||
| const response = status | ||
| ? ({ status, data: { data: null, code: 'WISH-004' } } as AxiosResponse<ApiErrorResponseT>) | ||
| : void 0; | ||
|
|
||
| return new AxiosError<ApiErrorResponseT>('failed', code, config, void 0, response); | ||
| }; | ||
|
|
||
| const captured = () => vi.mocked(Sentry.captureException).mock.calls; | ||
|
|
||
| describe('captureApiError', () => { | ||
| it('5xx 는 메서드·status·경로로 ApiError 를 보낸다', () => { | ||
| vi.mocked(Sentry.captureException).mockClear(); | ||
|
|
||
| captureApiError(axiosError({ code: 'ERR_BAD_RESPONSE', status: 500 }), 'api-client'); | ||
|
|
||
| const [error, context] = captured()[0] ?? []; | ||
| expect((error as Error).name).toBe('ApiError'); | ||
| expect((error as Error).message).toBe('API 500 POST /api/v1/wishes/123'); | ||
| expect(context).toMatchObject({ | ||
| tags: { source: 'api-client', errorType: 'server' }, | ||
| fingerprint: ['api', 'POST', '500', '/api/v1/wishes/:id'], | ||
| }); | ||
| }); | ||
|
|
||
| it('응답 없는 서버 네트워크 오류는 ERR_NETWORK 가 아니어도 보낸다', () => { | ||
| vi.mocked(Sentry.captureException).mockClear(); | ||
|
|
||
| captureApiError(axiosError({ code: 'ECONNREFUSED' }), 'api-server'); | ||
| captureApiError(axiosError({ code: 'ECONNABORTED' }), 'api-client'); | ||
|
|
||
| expect(captured().map(([error]) => (error as Error).message)).toEqual([ | ||
| 'API ECONNREFUSED POST /api/v1/wishes/123', | ||
| 'API ECONNABORTED POST /api/v1/wishes/123', | ||
| ]); | ||
| expect(captured()[0]?.[1]).toMatchObject({ tags: { errorType: 'network' } }); | ||
| }); | ||
|
|
||
| it('4xx 와 취소된 요청은 보내지 않는다', () => { | ||
| vi.mocked(Sentry.captureException).mockClear(); | ||
|
|
||
| captureApiError(axiosError({ code: 'ERR_BAD_REQUEST', status: 401 }), 'api-client'); | ||
| captureApiError(axiosError({ code: 'ERR_BAD_REQUEST', status: 404 }), 'api-client'); | ||
| captureApiError(axiosError({ code: 'ERR_CANCELED' }), 'api-client'); | ||
|
|
||
| expect(captured()).toHaveLength(0); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,16 +1,60 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
| import type { AxiosError } from 'axios'; | ||
| import { isAxiosError } from 'axios'; | ||
|
|
||
| import type { ApiErrorResponseT } from '@/types/api'; | ||
|
|
||
| type CaptureErrorContextT = { | ||
| /** 검색/그룹핑용 태그 (예: { source: 'api-client' }) */ | ||
| tags?: Record<string, string>; | ||
| /** 디버깅용 부가 정보 (요청 URL, status 등) */ | ||
| extra?: Record<string, unknown>; | ||
| fingerprint?: string[]; | ||
| }; | ||
|
|
||
| /** 잡은(caught) 에러를 Sentry에 수동 리포트 — 태그/컨텍스트를 일관되게 부착 */ | ||
| export const captureError = (error: unknown, context?: CaptureErrorContextT) => { | ||
| Sentry.captureException(error, { | ||
| tags: context?.tags, | ||
| extra: context?.extra, | ||
| fingerprint: context?.fingerprint, | ||
| }); | ||
| }; | ||
|
|
||
| const isServerOriginError = (error: unknown) => | ||
| error instanceof Error && 'digest' in error && typeof error.digest === 'string'; | ||
|
|
||
| /** API(axios) 에러는 인터셉터(apis/client.ts·server.ts)가 5xx·네트워크만 단독 수집 — 여기선 그 외 에러만 보낸다 */ | ||
| export const captureNonApiError = (error: unknown) => { | ||
| if (isAxiosError(error) || isServerOriginError(error)) return; | ||
| Sentry.captureException(error); | ||
| }; | ||
|
|
||
| /** fingerprint 용 — 리소스 ID 가 달라도 같은 엔드포인트면 한 이슈로 묶는다 */ | ||
| export const normalizeApiPath = (path: string) => | ||
| path.replace(/\/\d+(?=\/|$)/g, '/:id').replace(/\/[0-9a-f]{8}-[0-9a-f-]{27}(?=\/|$)/gi, '/:id'); | ||
|
|
||
| /** 5xx 와 응답 없는 네트워크 오류만 수집 */ | ||
| export const captureApiError = (error: AxiosError<ApiErrorResponseT>, source: string) => { | ||
| const status = error.response?.status; | ||
| if (error.code === 'ERR_CANCELED' || (typeof status === 'number' && status < 500)) return; | ||
|
|
||
| const method = error.config?.method?.toUpperCase() ?? 'UNKNOWN'; | ||
| const path = error.config?.url?.split('?')[0] ?? 'unknown'; | ||
| const reason = String(status ?? error.code ?? 'NETWORK'); | ||
|
|
||
| const apiError = new Error(`API ${reason} ${method} ${path}`); | ||
| apiError.name = 'ApiError'; | ||
|
|
||
| captureError(apiError, { | ||
| tags: { source, errorType: status ? 'server' : 'network' }, | ||
| fingerprint: ['api', method, reason, normalizeApiPath(path)], | ||
| extra: { | ||
| url: error.config?.url, | ||
| method: error.config?.method, | ||
| status, | ||
| code: error.code, | ||
| apiCode: error.response?.data?.code, | ||
| }, | ||
| }); | ||
| }; | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.