From 31634e7f62d35a431808b14d2c08f3080bfba760 Mon Sep 17 00:00:00 2001 From: chanwoo7 Date: Thu, 28 May 2026 04:33:09 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20throw=20new=20Error=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=98=A4=EB=A5=98=20=EC=A0=95=EB=A6=AC=20?= =?UTF-8?q?(P1-2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 도메인 입력 오류를 throw new Error 대신 HttpException 으로 변환. boot-time fail-fast(config/JWT_ACCESS_SECRET 누락 등)는 그대로 유지. - oidc-provider.type.ts: throw new Error → BadRequestException (FE 응답 500→400) - 호출 컨텍스트: HTTP request path (auth.controller 콜백 + oidc-login.service) - 협의 #3 대상 — 500 retry 로직이 있다면 제거 필요 - id-parser.ts / parse-account-id.ts: 같은 함수 내 try/catch 제어흐름용 throw new Error 'empty'/'negative' 제거. BadRequestException 직접 throw 로 단순화. 외부 동작 동일 (이미 catch 에서 BadRequestException 으로 변환됨). --- src/common/utils/id-parser.ts | 19 +++++++------- .../auth/types/oidc-provider.type.spec.ts | 9 +++++-- src/features/auth/types/oidc-provider.type.ts | 8 +++++- src/global/auth/parse-account-id.ts | 25 ++++++++++--------- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/common/utils/id-parser.ts b/src/common/utils/id-parser.ts index eb38f0c..97acebf 100644 --- a/src/common/utils/id-parser.ts +++ b/src/common/utils/id-parser.ts @@ -1,17 +1,18 @@ import { BadRequestException } from '@nestjs/common'; export function parseId(raw: string): bigint { + const trimmed = raw.trim(); + if (trimmed === '') { + throw new BadRequestException('Invalid id.'); + } + let id: bigint; try { - const trimmed = raw.trim(); - if (trimmed === '') { - throw new Error('empty'); - } - const id = BigInt(trimmed); - if (id < 0n) { - throw new Error('negative'); - } - return id; + id = BigInt(trimmed); } catch { throw new BadRequestException('Invalid id.'); } + if (id < 0n) { + throw new BadRequestException('Invalid id.'); + } + return id; } diff --git a/src/features/auth/types/oidc-provider.type.spec.ts b/src/features/auth/types/oidc-provider.type.spec.ts index 96f69e1..182afe8 100644 --- a/src/features/auth/types/oidc-provider.type.spec.ts +++ b/src/features/auth/types/oidc-provider.type.spec.ts @@ -1,3 +1,5 @@ +import { BadRequestException } from '@nestjs/common'; + import { parseOidcProvider } from '@/features/auth/types/oidc-provider.type'; describe('parseOidcProvider', () => { @@ -9,17 +11,20 @@ describe('parseOidcProvider', () => { expect(parseOidcProvider('kakao')).toBe('kakao'); }); - it('지원하지 않는 provider이면 에러를 던진다', () => { + it('지원하지 않는 provider이면 BadRequestException을 던진다', () => { + expect(() => parseOidcProvider('facebook')).toThrow(BadRequestException); expect(() => parseOidcProvider('facebook')).toThrow( 'Unsupported OIDC provider: facebook', ); }); - it('빈 문자열이면 에러를 던진다', () => { + it('빈 문자열이면 BadRequestException을 던진다', () => { + expect(() => parseOidcProvider('')).toThrow(BadRequestException); expect(() => parseOidcProvider('')).toThrow('Unsupported OIDC provider: '); }); it('대소문자를 구분한다', () => { + expect(() => parseOidcProvider('Google')).toThrow(BadRequestException); expect(() => parseOidcProvider('Google')).toThrow( 'Unsupported OIDC provider: Google', ); diff --git a/src/features/auth/types/oidc-provider.type.ts b/src/features/auth/types/oidc-provider.type.ts index 39ed64d..3d8a2dd 100644 --- a/src/features/auth/types/oidc-provider.type.ts +++ b/src/features/auth/types/oidc-provider.type.ts @@ -1,3 +1,5 @@ +import { BadRequestException } from '@nestjs/common'; + /** * 지원하는 OIDC Provider 타입 */ @@ -6,10 +8,14 @@ export type OidcProvider = 'google' | 'kakao'; /** * OIDC Provider 파라미터를 검증하고 타입으로 변환한다. * + * 호출 컨텍스트: HTTP request path (auth.controller 콜백, oidc-login.service). + * 따라서 잘못된 값은 도메인 입력 오류로 분류해 4xx 로 반환한다. + * * @param raw provider 문자열 * @returns provider 타입 + * @throws BadRequestException 지원하지 않는 provider 인 경우 */ export function parseOidcProvider(raw: string): OidcProvider { if (raw === 'google' || raw === 'kakao') return raw; - throw new Error(`Unsupported OIDC provider: ${raw}`); + throw new BadRequestException(`Unsupported OIDC provider: ${raw}`); } diff --git a/src/global/auth/parse-account-id.ts b/src/global/auth/parse-account-id.ts index c14f582..684f765 100644 --- a/src/global/auth/parse-account-id.ts +++ b/src/global/auth/parse-account-id.ts @@ -3,20 +3,21 @@ import { BadRequestException } from '@nestjs/common'; import type { JwtUser } from '@/global/auth/types/jwt-payload.type'; export function parseAccountId(user: JwtUser): bigint { + const raw = + typeof user.accountId === 'string' + ? user.accountId.trim() + : String(user.accountId ?? ''); + if (raw === '') { + throw new BadRequestException('Invalid account id.'); + } + let id: bigint; try { - const raw = - typeof user.accountId === 'string' - ? user.accountId.trim() - : String(user.accountId ?? ''); - if (raw === '') { - throw new Error('empty'); - } - const id = BigInt(raw); - if (id < 0n) { - throw new Error('negative'); - } - return id; + id = BigInt(raw); } catch { throw new BadRequestException('Invalid account id.'); } + if (id < 0n) { + throw new BadRequestException('Invalid account id.'); + } + return id; }