Skip to Content
OpenPerf

Guides

Capture errors

Report handled errors and add the user, tags and breadcrumbs that explain them.

Uncaught errors reach OpenPerf on their own. Call captureException for the errors your code catches, and add context so each error says who it happened to and what led up to it.

import { captureException } from "@openperf/sdk/browser";

try {
  JSON.parse("{ not json");
} catch (error) {
  captureException(error);
}

On Node.js and Bun, import the same functions from @openperf/sdk/node. The error shows up under Errors as Handled, linked to the trace that was running and with the user, tags and breadcrumbs set at that moment.

What is captured without a call

  • Uncaught errors and unhandled promise rejections, in the browser and on the server. They are marked Unhandled.
  • An error that escapes a startSpan callback.
  • On the server, errors that a framework records on a request, such as an error thrown by an Express route or a Bun.serve handler.

An error object that is both thrown from a span and passed to captureException is counted once.

Add the user

import { setUser } from "@openperf/sdk/browser";

setUser({ id: "42", email: "ada@example.com", name: "Ada" });

Every field is optional. Later errors and spans carry the user. setUser(null) clears it, for example at sign-out.

Add tags

import { setTag } from "@openperf/sdk/browser";

setTag("plan", "pro");

A tag becomes an attribute on later errors and spans. The value is a string, a number or a boolean. When a span has its own attribute with the same key, the span's value wins.

Leave breadcrumbs

A breadcrumb is a step that happened before an error.

import { addBreadcrumb } from "@openperf/sdk/browser";

addBreadcrumb({
  category: "cart",
  message: "Coupon applied",
  data: { code: "SPRING" },
});

Only category is required. level defaults to info. data takes strings, numbers and booleans. Other values and empty strings are dropped. Uncaught errors and errors reported with captureException carry the last 100 breadcrumbs, shown under Breadcrumbs on the error.

In the browser the SDK also leaves breadcrumbs itself: clicks, console output, route changes and requests.

Change grouping and the handled state

The second argument of captureException takes two options.

import { captureException } from "@openperf/sdk/browser";

captureException(new Error("Card declined"), { fingerprint: "payment-declined" });
  • fingerprint puts all errors with the same value into one group, instead of grouping by type and stack.
  • handled: false marks the error as Unhandled. The default is true.

Context on the server

In the browser, the user, tags and breadcrumbs apply to the whole page. On Node.js and Bun they follow the trace:

  • A call made inside a request applies to that request only.
  • A call made outside any trace, for example at startup, applies to every request that starts later.

Limits

  • In the browser, the same error is sent once a minute at most, and no more than 100 different errors are sent per minute.
  • Up to five causes from error.cause and AggregateError.errors are added to the stack trace.
  • A thrown primitive, such as a string, is reported with its text as the message.
  • Before init runs, or when it has no DSN, every function does nothing.

On this page