Skip to main content
The captureException function captures an exception or error and sends it to Sentry.

Function Signature

Parameters

unknown
required
The exception to capture. Can be an Error object, string, or any value.
CaptureContext
Additional context for the exception. Can be:
  • A Scope object
  • A partial ScopeContext object
  • A callback function receiving the current scope

Returns

string
The unique ID of the captured event.

Basic Usage

With Context

Using Object Context

Using Scope Callback

Advanced Examples

With User Context

With Fingerprinting

Control how errors are grouped in Sentry:

With Attachments

Implementation Details

From packages/core/src/client.ts:

Event Processing

Exceptions go through several processing steps:
  1. Duplicate Check: Ensures the same error object isn’t captured twice
  2. Event Creation: Converts the exception into an event
  3. Scope Application: Applies scope data (breadcrumbs, tags, context)
  4. Event Processors: Runs through event processors
  5. Sampling: Applies sample rate
  6. beforeSend: Runs the beforeSend callback
  7. Transport: Sends to Sentry via transport

Error Types

Error Objects

Custom Errors

Non-Error Values

Best Practices

1. Always Include Context

2. Use Appropriate Severity Levels

3. Add Breadcrumbs for Context

4. Handle Async Errors