ErrorBoundary
먼저 보고, 그다음 계약을 읽습니다
렌더 중 throw 를 격리·복구하는 React 에러 경계.
Component intent의도와 경계 읽기
자식 트리의 렌더 도중 발생한 throw 를 잡아 트리 전체 white-out 대신 복구 가능한 fallback 을 보여주는 class 컴포넌트. 기본 fallback 은 잡은 error 를 humanizeApiError 로 {title, detail} 로 변환해 Callout(variant="danger") + '다시 시도' Button 으로 렌더한다(한국어 사용자 메시지). fallback prop 으로 직접 렌더를 대체하고, onError 로 Sentry/console/datadog 로깅을 붙이며, resetKeys 원소가 바뀌면(예 pathname·query·userId) 자동으로 reset 된다. getDerivedStateFromError/componentDidCatch 는 hooks 로 대체 불가라 class + "use client" 로 구현.
예제
SSOT에 등록된 실제 API 기준 snippet입니다. standalone 배지만 독립 실행 단위이며, fragment는 주변 state·handler 문맥을 생략합니다.
import { ErrorBoundary } from "@axe/ui";import { usePathname } from "next/navigation";function Page({ children }: { children: React.ReactNode }) { const pathname = usePathname(); return ( <ErrorBoundary resetKeys={[pathname]} onError={(error, info) => reportToSentry(error, info)} > {children} </ErrorBoundary> );}import { ErrorBoundary } from "@axe/ui";<ErrorBoundary fallback={(error, reset) => ( <div role="alert"> <p>차트를 그리지 못했어요: {error.message}</p> <button onClick={reset}>다시 시도</button> </div> )}> <StreamingChart /></ErrorBoundary>Props
TSX 소스가 진실입니다. 주요 export 컴포넌트의 public API만 노출합니다.
| 이름 | 타입 | 필수 | 기본값 | 설명 |
|---|---|---|---|---|
children | React.ReactNode | 필수 | — | 감쌀 트리. 이 안의 렌더 throw 만 포착된다. |
fallback | (error: Error, reset: () => void) => React.ReactNode | — | — | 받으면 기본 Callout fallback 을 대체. reset 을 호출하면 boundary 가 복구. |
onError | (error: Error, errorInfo: React.ErrorInfo) => void | — | — | 외부 로깅 hook — componentDidCatch 에서 호출(Sentry/console/datadog). |
resetKeys | unknown[] | — | — | 원소 중 하나라도 Object.is 로 바뀌면 자동 reset(라우트/쿼리 변경 패턴). |
.axe-* 클래스 계약
React 밖에서도 같은 표면을 그리는 공개 계약입니다. stable은 minor 버전 안에서 이름을 바꾸지 않습니다.
| 클래스 | 안정성 | 용도 |
|---|---|---|
.axe-error-boundary | stable | 기본 fallback wrapper(spacing 컬럼). custom fallback 시엔 렌더 안 됨. |
.axe-error-boundary__detail | internal | humanizeApiError 의 detail 문단(기본 fallback 내부). |
.axe-error-boundary__actions | internal | '다시 시도' 액션 row(기본 fallback 내부). |
접근성
키보드, ARIA, 구현 노트를 함께 검토합니다.
- 기본 fallback 의 '다시 시도' 버튼 — Enter/Space(네이티브 <button>)
boundary 자체는 role 이 없다(순수 로직). 기본 fallback 의 시맨틱은 Callout(variant="danger")과 Button 에 위임된다. custom fallback 을 쓰면 접근성 표지는 caller 책임.
componentDidCatch → onError 로 오류를 흘리고, resetKeys 원소가 Object.is 로 바뀌면 componentDidUpdate 가 자동 복구한다. 포착 범위는 React 에러 경계 한계와 동일 — 렌더/라이프사이클 중 throw 만 잡고, 이벤트 핸들러·비동기·SSR 밖 오류는 잡지 못한다.