Skip to main content

errors

How to define and check error classes in Ledger Live. Read when adding a new error, creating an errors.ts file, or checking an error's type.

소스 정보

저장소
LedgerHQ/ledger-live
최근 소스 활동
2026년 8월 7일 09:20
감지된 SKILL.md 언어
영어
스타
622
포크
491

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
errors
description
How to define and check error classes in Ledger Live. Read when adding a new error, creating an errors.ts file, or checking an error's type.
globs
["**/errors.ts","**/errors/**/*.ts"]
# Errors Each package owns its errors in its own `src/errors.ts`, as plain native classes. There is no shared errors lib: the former `@ledgerhq/errors` package has been removed. ## Define errors A package groups its errors in `src/errors.ts`. Extend `Error` directly and set a stable `name`. The `name` string is the contract — keep it stable, it is what survives across process/serialization boundaries. ```ts // message-only export class FooNotFound extends Error { override name = "FooNotFound"; } // with a default message export class InvalidChallenge extends Error { override name = "InvalidChallenge"; constructor() { super("backend returned an invalid challenge"); } } // with extra fields export class HttpError extends Error { override name = "HttpError"; readonly status: number | undefined; constructor(message: string, status?: number) { super(message); this.status = status; } } ``` Group a family under a per-package base class so callers can catch the whole set: ```ts export class WalletAuthError extends Error { override name = "WalletAuthError"; } export class WalletAuthSignatureError extends WalletAuthError { override name = "WalletAuthSignatureError"; constructor(cause: unknown) { super("failed to sign challenge", { cause }); } } ``` See `libs/ledger-auth/src/errors.ts` for a full reference. ### `cause` Use the native second argument: `super(message, { cause })`. Error `cause` was standardized in ES2022 and is supported by modern runtimes; its TypeScript typing (`ErrorOptions`) requires `lib: es2022.error` (i.e. `es2022`). Coin modules and some older packages still target `es2020` — there, declare and assign manually: ```ts export class BroadcastError extends Error { override name = "BroadcastError"; cause?: unknown; constructor(message: string, cause?: unknown) { super(message); this.cause = cause; } } ``` ## Check error type: prefer `error.name` Prefer `error.name === "FooNotFound"` over `instanceof FooNotFound`. `instanceof` breaks once an error crosses a boundary — desktop/mobile IPC, worker messaging, or the external `@ledgerhq/coin-module-framework` — because the object is rebuilt and loses its prototype. The `name` string always survives. ```ts // ✅ survives serialization if (error instanceof Error && error.name === "FooNotFound") { … } // ⚠️ only works in-process, before any serialization if (error instanceof FooNotFound) { … } ``` Use `instanceof` only when you are certain the error has not crossed a boundary (e.g. caught in the same module that threw it) and you need the typed fields. ## Where errors live - Logic in one package → that package's `src/errors.ts`. - Used only by an app → the app's `src/errors.ts` (`apps/ledger-live-desktop`, `apps/ledger-live-mobile`). - Shared by several coin modules → `libs/ledger-wallet-framework/src/errors.ts` (`@ledgerhq/ledger-wallet-framework/errors`), the shared home below the coin layer.
GitHub에서 보기