JavaScript code wraps errors all the time. A database driver throws a low-level error, a data layer catches the low-level error and throws a friendlier one, and a route handler does the same again. Since ES2022, JavaScript has had a standard place to keep the original error: the cause property. The error at the end of a cause chain is usually the error that explains the failure.

We recently released version 9.2 of the BugSplat JavaScript SDK, which reports the whole Error.cause chain. When your app posts an error, the crash report shows every linked error with its own call stack, and the root cause names the crash. Error chains work in browser and Node.js apps, and in the BugSplat SDKs for React, Angular, Expo, and Electron.

What's New

  • Error.cause chains are captured by post() with nothing to configure.
  • The root cause names the crash. The Exception Code and Exception Message come from the innermost error.
  • A call stack for every linked error, each under its own Caused by: row.
  • Error properties as locals, such as a database driver's code and constraint_name.
  • Properties of the error you posted become attributes that you can search and filter by.
  • Source maps apply to every linked call stack.
  • AggregateError support.
  • Real function names for async frames, constructors, and aliased methods.

A Wrapped Database Error

Say your sign-up route wraps a database failure before rethrowing the failure:

async function createAccount(user: NewUser) {
  try {
    return await sql`insert into users ${sql(user)}`;
  } catch (error) {
    throw new Error('Failed to create account', { cause: error });
  }
}

When the wrapped error reaches bugsplat.post(), the crash report lists the call stack of the error you posted, starting at createAccount. A Caused by: PostgresError row follows, and then the call stack of the PostgresError.

Here are the details of the crash report for a sign-up with an email address that already exists. The Exception Code and the Exception Message come from the PostgresError:

The Details section of a BugSplat crash report, with the group createAccount, the Exception Code PostgresError and the Exception Message

What a Crash Report Contains

  • The root cause names the crash. The Exception Code and Exception Message come from the innermost error. Your list of crash reports shows PostgresError and duplicate key value violates unique constraint "users_email_key", where a report of the outermost error alone would show Error and Failed to create account.
  • Every linked error keeps its own call stack. Each linked error follows a Caused by: row, so you can see where each layer of your code threw.
  • Properties become locals. Expand a Caused by: row to see the linked error's message and own properties, such as a database driver's code, constraint_name, or detail.
  • Properties of the error you posted become attributes. Properties you attach before rethrowing, such as a requestId or a route, are added to the crash report's attributes, so you can search and filter by requestId or route. Attributes you pass to post() take precedence.
  • Source maps apply to every call stack. If you upload source maps for the version, the call stack of each linked error is mapped back to your original source.

Here is the call stack of the same crash report, with the Caused by: PostgresError row expanded to show the locals:

The Stack section of a BugSplat crash report. The call stack of the posted error starts at createAccount. The row

An error chain also follows the errors of an AggregateError, such as an AggregateError from Promise.any. A cause that is a string or a plain object is recorded with its type and message.

How Error Chains Affect Grouping

BugSplat still groups a crash by the function name and line number of the error you posted, so error chains do not move your existing crashes into new groups.

A Caused by: row is named for the error's type only, and the error's message appears in the row's locals. Two crashes whose causes differ only in an IP address, a URL, or a record ID stay in one group.

Auto-grouping rules apply across the chain. If an auto-grouping rule ignores every frame of the error you posted, for example a rule that ignores **/node_modules/**, grouping continues into the call stacks of the linked errors and skips the Caused by: rows.

Function Names for Async Frames

Call stacks from V8 now show the real function name for async frames, for constructors such as new PostgresError, and for methods called through an alias. New crashes with an async frame, a constructor frame, or an aliased method frame are grouped under the real function name, so an existing async(...) or new(...) group stops growing.

Get Started

Error chains require version 9.2.0 or later of the bugsplat package:

npm install bugsplat@latest
  • Node.js: import BugSplatNode from bugsplat/node.
  • React: @bugsplat/react reports errors through bugsplat. Run npm update so that your lockfile resolves bugsplat 9.2.0 or later.
  • Angular: install the new @bugsplat/angular package, which replaces bugsplat-ng and requires Angular 22 or later. The Angular guide has the setup steps.
  • Expo: @bugsplat/expo reports errors through @bugsplat/react, so the same npm update applies.
  • Electron: @bugsplat/electron captures error chains in the main process and in every renderer. The Electron guide has the setup steps.

The Error Chains section of the JavaScript guide covers error chains in more detail. If you see an error chain that BugSplat reports badly, email support@bugsplat.com and tell us what happened.