JavaScript theorytheory 0/50 · 0%
Safety · easy

15. Errors

throw, Error objects, cause and custom classes.

Throw `Error` objects, not strings, so you keep a stack trace. Subclass `Error` for domain failures and use `{ cause }` to keep the original.

class RpcError extends Error {
  constructor(message, options) { super(message, options); this.name = "RpcError"; }
}
throw new RpcError("call failed", { cause: err });

Never swallow an error silently — log it or rethrow, otherwise debugging a failed transaction becomes guesswork.

Check your understanding

  1. 1. Why throw Error objects rather than strings?

  2. 2. What does `{ cause }` provide?

  3. 3. What is an anti-pattern?