Communitygithub.com

오류 처리 패턴

TypeScript, Python, Go 전반에 걸친 강력한 오류 처리 패턴. 타입 오류, 오류 경계, 재시도, 차단기, 사용자 대상 오류 메시지를 다룹니다. TypeScript, Python, Go에서 오류 타입 설계, 재시도, 차단기, 사용자 대상 실패 메시지 작업 시 사용하세요.

오류 처리 패턴란 무엇인가요?

이 스킬은 TypeScript, Python, Go 프로젝트에서 일관되고 강건한 오류 처리를 구현하기 위한 포괄적인 패턴을 제공합니다. 핵심 원칙으로 빠르고 명확한 실패(fail fast and loudly), 타입화된 오류, 사용자 메시지와 개발자 메시지 분리, 오류 무시 금지, 오류를 API 계약의 일부로 취급하는 방법을 제시합니다. 각 언어별로 구체적인 구현 예제를 포함하며, TypeScript의 경우 계층적 오류 클래스, Result 패턴, Next.js/Express API 오류 처리기, React 오류 경계(ErrorBoundary)를 다룹니다. Python은 FastAPI 글로벌 예외 처리기와 함께 사용자 정의 예외 계층 구조를, Go는 센티넬 오류 및 오류 래핑(wrapping) 패턴을 제시합니다. 또한 지수 백오프를 이용한 재시도 로직과 사용자 친화적인 오류 메시지 매핑, 오류 처리 체크리스트를 포함하여 프로덕션 환경에서 즉시 적용 가능한 실용적인 지침을 제공합니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/ECC/tree/main/skills/error-handling

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

오류 처리 패턴은(는) 무엇을 하나요?

프로덕션 애플리케이션을 위한 일관되고 강력한 오류 처리 패턴입니다.

활성화 시점

  • 새 모듈이나 서비스를 위한 오류 타입 또는 예외 계층 구조를 설계할 때
  • 신뢰할 수 없는 외부 의존성에 재시도 로직이나 회로 차단기를 추가할 때
  • API 엔드포인트에서 오류 처리가 누락되었는지 검토할 때
  • 사용자 대상 오류 메시지 및 피드백을 구현할 때
  • 연쇄 실패나 조용한 오류 무시를 디버깅할 때

핵심 원칙

  1. 빠르고 명확하게 실패하라 — 오류가 발생한 경계에서 표면화하라; 감추지 마라
  2. 문자열 메시지보다 타입화된 오류 — 오류는 구조를 가진 일급 값이다
  3. 사용자 메시지 ≠ 개발자 메시지 — 사용자에게는 친숙한 텍스트를 보여주고, 서버 측에는 전체 컨텍스트를 로깅하라
  4. 절대 오류를 조용히 삼키지 마라 — 모든 catch 블록은 처리하거나, 다시 던지거나, 로깅해야 한다
  5. 오류는 API 계약의 일부이다 — 클라이언트가 받을 수 있는 모든 오류 코드를 문서화하라

TypeScript / JavaScript

타입화된 오류 클래스

// 도메인에 대한 오류 계층 구조 정의
class AppError extends Error {
  constructor(
    message: string,
    public readonly code: string,
    public readonly statusCode: number = 500,
    public readonly details?: unknown,
  ) {
    super(message)
    this.name = this.constructor.name
    // 트랜스파일된 ES5 JavaScript에서 올바른 프로토타입 체인 유지.
    // 내장 Error 클래스를 확장할 때 `instanceof` 검사(예: `error instanceof NotFoundError`)가
    // 올바르게 작동하도록 하는 데 필요합니다.
    Object.setPrototypeOf(this, new.target.prototype)
  }
}

class NotFoundError extends AppError {
  constructor(resource: string, id: string) {
    super(`${resource} not found: ${id}`, 'NOT_FOUND', 404)
  }
}

class ValidationError extends AppError {
  constructor(message: string, details: { field: string; message: string }[]) {
    super(message, 'VALIDATION_ERROR', 422, details)
  }
}

class UnauthorizedError extends AppError {
  constructor(reason = 'Authentication required') {
    super(reason, 'UNAUTHORIZED', 401)
  }
}

class RateLimitError extends AppError {
  constructor(public readonly retryAfterMs: number) {
    super('Rate limit exceeded', 'RATE_LIMITED', 429)
  }
}

Result 패턴 (throw 없는 스타일)

실패가 예상되고 흔한 작업(구문 분석, 외부 호출)의 경우:

type Result<T, E = AppError> =
  | { ok: true; value: T }
  | { ok: false; error: E }

function ok<T>(value: T): Result<T> {
  return { ok: true, value }
}

function err<E>(error: E): Result<never, E> {
  return { ok: false, error }
}

// 사용법
async function fetchUser(id: string): Promise<Result<User>> {
  try {
    const user = await db.users.findUnique({ where: { id } })
    if (!user) return err(new NotFoundError('User', id))
    return ok(user)
  } catch (e) {
    return err(new AppError('Database error', 'DB_ERROR'))
  }
}

const result = await fetchUser('abc-123')
if (!result.ok) {
  // TypeScript는 여기서 result.error를 알고 있음
  logger.error('Failed to fetch user', { error: result.error })
  return
}
// TypeScript는 여기서 result.value를 알고 있음
console.log(result.value.email)

API 오류 처리기 (Next.js / Express)

import { NextRequest, NextResponse } from 'next/server'

function handleApiError(error: unknown): NextResponse {
  // 알려진 애플리케이션 오류
  if (error instanceof AppError) {
    return NextResponse.json(
      {
        error: {
          code: error.code,
          message: error.message,
          ...(error.details ? { details: error.details } : {}),
        },
      },
      { status: error.statusCode },
    )
  }

  // Zod 유효성 검사 오류
  if (error instanceof z.ZodError) {
    return NextResponse.json(
      {
        error: {
          code: 'VALIDATION_ERROR',
          message: 'Request validation failed',
          details: error.issues.map(i => ({
            field: i.path.join('.'),
            message: i.message,
          })),
        },
      },
      { status: 422 },
    )
  }

  // 예상치 못한 오류 — 세부 정보 로깅, 일반 메시지 반환
  console.error('Unexpected error:', error)
  return NextResponse.json(
    { error: { code: 'INTERNAL_ERROR', message: 'An unexpected error occurred' } },
    { status: 500 },
  )
}

export async function POST(req: NextRequest) {
  try {
    // ... 핸들러 로직
  } catch (error) {
    return handleApiError(error)
  }
}

React 오류 경계

import { Component, ErrorInfo, ReactNode } from 'react'

interface Props {
  fallback: ReactNode
  onError?: (error: Error, info: ErrorInfo) => void
  children: ReactNode
}

interface State {
  hasError: boolean
  error: Error | null
}

class ErrorBoundary extends Component<Props, State> {
  state: State = { hasError: false, error: null }

  static getDerivedStateFromError(error: Error): State {
    return { hasError: true, error }
  }

  componentDidCatch(error: Error, info: ErrorInfo) {
    this.props.onError?.(error, info)
    console.error('Unhandled React error:', error, info)
  }

  render() {
    if (this.state.hasError) return this.props.fallback
    return this.props.children
  }
}

// 사용법
<ErrorBoundary fallback={<p>문제가 발생했습니다. 새로고침 해주세요.</p>}>
  <MyComponent />
</ErrorBoundary>

Python

사용자 정의 예외 계층 구조

class AppError(Exception):
    """기본 애플리케이션 오류."""
    def __init__(self, message: str, code: str, status_code: int = 500):
        super().__init__(message)
        self.code = code
        self.status_code = status_code

class NotFoundError(AppError):
    def __init__(self, resource: str, id: str):
        super().__init__(f"{resource} not found: {id}", "NOT_FOUND", 404)

class ValidationError(AppError):
    def __init__(self, message: str, details: list[dict] | None = None):
        super().__init__(message, "VALIDATION_ERROR", 422)
        self.details = details or []

FastAPI 글로벌 예외 처리기

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.exception_handler(AppError)
async def app_error_handler(request: Request, exc: AppError) -> JSONResponse:
    return JSONResponse(
        status_code=exc.status_code,
        content={"error": {"code": exc.code, "message": str(exc)}},
    )

@app.exception_handler(Exception)
async def generic_error_handler(request: Request, exc: Exception) -> JSONResponse:
    # 전체 세부 정보 로깅, 일반 메시지 반환
    logger.exception("Unexpected error", exc_info=exc)
    return JSONResponse(
        status_code=500,
        content={"error": {"code": "INTERNAL_ERROR", "message": "An unexpected error occurred"}},
    )

Go

센티넬 오류 및 오류 래핑

package domain

import "errors"

// 타입 검사를 위한 센티넬 오류
var (
    ErrNotFound    = errors.New("not found")
    ErrUnauthorized = errors.New("unauthorized")
    ErrConflict     = errors.New("conflict")
)

// 컨텍스트로 오류 래핑 — 원래 오류를 절대 잃지 마십시오
func (r *UserRepository) FindByID(ctx context.Context, id string) (*User, error) {
    user, err := r.db.QueryRow(ctx, "SELECT * FROM users WHERE id = $1", id)
    if errors.Is(err, sql.ErrNoRows) {
        return nil, fmt.Errorf("user %s: %w", id, ErrNotFound)
    }
    if err != nil {
        return nil, fmt.Errorf("querying user %s: %w", id, err)
    }
    return user, nil
}

// 핸들러 수준에서 래핑을 풀어 응답 결정
func (h *Handler) GetUser(w http.ResponseWriter, r *http.Request) {
    user, err := h.service.GetUser(r.Context(), chi.URLParam(r, "id"))
    if err != nil {
        switch {
        case errors.Is(err, domain.ErrNotFound):
            writeError(w, http.StatusNotFound, "not_found", err.Error())
        case errors.Is(err, domain.ErrUnauthorized):
            writeError(w, http.StatusForbidden, "forbidden", "Access denied")
        default:
            slog.Error("unexpected error", "err", err)
            writeError(w, http.StatusInternalServerError, "internal_error", "An unexpected error occurred")
        }
        return
    }
    writeJSON(w, http.StatusOK, user)
}

지수 백오프를 이용한 재시도

interface RetryOptions {
  maxAttempts?: number
  baseDelayMs?: number
  maxDelayMs?: number
  retryIf?: (error: unknown) => boolean
}

async function withRetry<T>(
  fn: () => Promise<T>,
  options: RetryOptions = {},
): Promise<T> {
  const {
    maxAttempts = 3,
    baseDelayMs = 500,
    maxDelayMs = 10_000,
    retryIf = () => true,
  } = options

  let lastError: unknown

  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    try {
      return await fn()
    } catch (error) {
      lastError = error
      if (attempt === maxAttempts || !retryIf(error)) throw error

      const jitter = Math.random() * baseDelayMs
      const delay = Math.min(baseDelayMs * 2 ** (attempt - 1) + jitter, maxDelayMs)
      await new Promise(resolve => setTimeout(resolve, delay))
    }
  }

  throw lastError
}

// 사용법: 일시적인 네트워크 오류는 재시도하지만, 4xx 오류는 재시도하지 않음
const data = await withRetry(() => fetch('/api/data').then(r => r.json()), {
  maxAttempts: 3,
  retryIf: (error) => !(error instanceof AppError && error.statusCode < 500),
})

사용자 대상 오류 메시지

오류 코드를 사람이 읽을 수 있는 메시지로 매핑합니다. 기술적 세부 사항은 사용자에게 보이는 텍스트에서 제외하세요.

const USER_ERROR_MESSAGES: Record<string, string> = {
  NOT_FOUND: '요청한 항목을 찾을 수 없습니다.',
  UNAUTHORIZED: '계속하려면 로그인해 주세요.',
  FORBIDDEN: '이 작업을 수행할 권한이 없습니다.',
  VALIDATION_ERROR: '입력을 확인하고 다시 시도해 주세요.',
  RATE_LIMITED: '요청이 너무 많습니다. 잠시 기다린 후 다시 시도해 주세요.',
  INTERNAL_ERROR: '저희 쪽에 문제가 발생했습니다. 나중에 다시 시도해 주세요.',
}

export function getUserMessage(code: string): string {
  return USER_ERROR_MESSAGES[code] ?? USER_ERROR_MESSAGES.INTERNAL_ERROR
}

오류 처리 체크리스트

오류 처리를 건드리는 코드를 병합하기 전에:

  • 모든 catch 블록이 처리, 재던지기, 또는 로깅 중 하나를 수행 — 조용히 삼키지 않음
  • API 오류가 표준 형식 { error: { code, message } }을 따름
  • 사용자 대상 메시지에 스택 트레이스나 내부 세부 정보가 포함되지 않음
  • 전체 오류 컨텍스트가 서버 측에 로깅됨
  • 사용자 정의 오류 클래스가 code 필드와 함께 기본 AppError를 확장함
  • 비동기 함수가 호출자에게 오류를 표면화 — 폴백 없이 fire-and-forget 사용 금지
  • 재시도 로직이 재시도 가능한 오류만 재시도 (4xx 클라이언트 오류 제외)
  • React 컴포넌트가 렌더링 오류에 대해 ErrorBoundary로 래핑됨

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/content-engine

Create platform-native content systems for X, LinkedIn, TikTok, YouTube, newsletters, and repurposed multi-platform campaigns. Use when the user wants social posts, threads, scripts, content calendars, or one source asset adapted cleanly across platforms.

affaan-m/ECC

Multi-platform content distribution across X, LinkedIn, Threads, and Bluesky. Adapts content per platform using content-engine patterns. Never posts identical content cross-platform. Use when the user wants to distribute content across social platforms.

affaan-m/fal-ai-media

Unified media generation via fal.ai MCP — image, video, and audio. Covers text-to-image (Nano Banana), text/image-to-video (Seedance, Kling, Veo 3), text-to-speech (CSM-1B), and video-to-audio (ThinkSound). Use when the user wants to generate images, videos, or audio with AI.

affaan-m/manim-video

日本語翻訳:このファイルは manim-video 用の日本語翻訳が必要です

affaan-m/remotion-video-creation

Remotion のベストプラクティス - React で動画を作成する。3D、アニメーション、音声、字幕、チャート、トランジションなどをカバーするドメイン固有の29のルール。

affaan-m/video-editing

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

agent-self-evaluation

Use after completing any non-trivial task. The agent self-rates its output on 5 axes — accuracy, completeness, clarity, actionability, conciseness — with concrete evidence per criterion. Produces a structured 1-5 scorecard with specific improvement suggestions.

agent-sort

Build an evidence-backed ECC install plan for a specific repo by sorting skills, commands, rules, hooks, and extras into DAILY vs LIBRARY buckets using parallel repo-aware review passes. Use when ECC should be trimmed to what a project actually needs instead of loading the full bundle.

ai-first-engineering

Engineering operating model for teams where AI agents generate a large share of implementation output. Use when setting team process, review gates, or ownership rules for a codebase largely written by agents.

ai-regression-testing

Regression testing strategies for AI-assisted development. Sandbox-mode API testing without database dependencies, automated bug-check workflows, and patterns to catch AI blind spots where the same model writes and reviews code. Use when adding regression coverage to AI-assisted code, or when the same model both wrote and reviewed a change.

android-clean-architecture

Clean Architecture patterns for Android and Kotlin Multiplatform projects — module structure, dependency rules, UseCases, Repositories, and data layer patterns. Use when structuring modules, layers, or data flow in an Android or KMP project.

angular-developer

Generates Angular code and provides architectural guidance. Trigger when creating projects, components, or services, or for best practices on reactivity (signals, linkedSignal, resource), forms, dependency injection, routing, SSR, accessibility (ARIA), animations, styling (component styles, Tailwind CSS), testing, or CLI tooling.

관련 스킬