Errors

From LedgerHQ/ledger-live · View interactive version

Errors is an AI agent skill from the GitHub repository LedgerHQ/ledger-live. 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.

How to use this skill

Skill file

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.

// 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. An excerpt of

CODE0, which holds

the full family:

export class ContactError extends Error {
  override name: string = "ContactError";
}
export class InvalidContactNameError extends ContactError {
  override name = "InvalidContactNameError" as const;
  constructor() {
    super("Expected letters, spaces, or digits");
  }
}

`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:

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.

// ✅ 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

CODE0. Mobile has no app-level errors file yet: add one at the

same place the first time it needs one.

(@ledgerhq/ledger-wallet-framework/errors), the shared home below the coin layer.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.