home / skills / thebushidocollective / han / error-capturing

This skill helps you capture errors with rich context in Sentry, including user, tags, extra data, and breadcrumbs for faster debugging.

npx playbooks add skill thebushidocollective/han --skill error-capturing

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.1 KB
---
name: sentry-error-capturing
description: Use when capturing and reporting errors to Sentry, adding context, or handling exceptions. Covers error boundaries, context enrichment, and fingerprinting.
allowed-tools:
  - Read
  - Write
  - Edit
  - Bash
  - Grep
  - Glob
---

# Sentry - Error Capturing & Context

Capture errors and enrich them with context for better debugging.

## Capturing Errors

### Manual Error Capture

```typescript
import * as Sentry from "@sentry/browser";

try {
  riskyOperation();
} catch (error) {
  Sentry.captureException(error);
}
```

### Capture with Extra Context

```typescript
Sentry.captureException(error, {
  tags: {
    section: "checkout",
    feature: "payment",
  },
  extra: {
    orderId: order.id,
    cartItems: cart.items.length,
  },
  level: "error",
});
```

### Capture Messages

```typescript
Sentry.captureMessage("User exceeded rate limit", {
  level: "warning",
  tags: { userId: user.id },
});
```

## Adding Context

### User Context

```typescript
Sentry.setUser({
  id: user.id,
  email: user.email,
  username: user.username,
  ip_address: "{{auto}}",
});

// Clear on logout
Sentry.setUser(null);
```

### Tags

```typescript
// Global tags
Sentry.setTag("app.version", "1.2.3");
Sentry.setTag("tenant", customer.tenantId);

// Per-event tags
Sentry.captureException(error, {
  tags: { operation: "database_query" },
});
```

### Extra Data

```typescript
Sentry.setExtra("orderDetails", {
  items: order.items,
  total: order.total,
});
```

### Context Objects

```typescript
Sentry.setContext("order", {
  id: order.id,
  status: order.status,
  items: order.items.length,
});

Sentry.setContext("customer", {
  plan: customer.plan,
  region: customer.region,
});
```

## Breadcrumbs

### Automatic Breadcrumbs

```typescript
// Most integrations add breadcrumbs automatically
// Console, fetch, XHR, DOM events, navigation
```

### Manual Breadcrumbs

```typescript
Sentry.addBreadcrumb({
  category: "auth",
  message: "User logged in",
  level: "info",
  data: {
    userId: user.id,
    method: "oauth",
  },
});
```

### Configure Breadcrumbs

```typescript
Sentry.init({
  beforeBreadcrumb(breadcrumb, hint) {
    // Filter or modify breadcrumbs
    if (breadcrumb.category === "console") {
      return null; // Don't capture console logs
    }
    return breadcrumb;
  },
  maxBreadcrumbs: 50,
});
```

## Error Boundaries (React)

```tsx
import * as Sentry from "@sentry/react";

// Basic error boundary
const App = () => (
  <Sentry.ErrorBoundary fallback={<ErrorPage />}>
    <YourApp />
  </Sentry.ErrorBoundary>
);

// With custom fallback and onError
<Sentry.ErrorBoundary
  fallback={({ error, resetError }) => (
    <div>
      <p>Something went wrong: {error.message}</p>
      <button onClick={resetError}>Try again</button>
    </div>
  )}
  onError={(error, componentStack) => {
    console.error("Caught by Sentry boundary:", error);
  }}
  beforeCapture={(scope) => {
    scope.setTag("location", "checkout");
  }}
>
  <CheckoutFlow />
</Sentry.ErrorBoundary>
```

## Fingerprinting

### Custom Grouping

```typescript
Sentry.captureException(error, {
  fingerprint: ["{{ default }}", user.id],
});
```

### Override Default Grouping

```typescript
Sentry.init({
  beforeSend(event) {
    if (event.exception?.values?.[0]?.type === "ChunkLoadError") {
      event.fingerprint = ["chunk-load-error"];
    }
    return event;
  },
});
```

## Scopes

### Configure Scope

```typescript
Sentry.configureScope((scope) => {
  scope.setUser({ id: user.id });
  scope.setTag("theme", "dark");
  scope.setLevel("warning");
});
```

### With Scope (Isolated)

```typescript
Sentry.withScope((scope) => {
  scope.setTag("operation", "batch_import");
  scope.setExtra("batchSize", items.length);
  Sentry.captureException(error);
});
// Tags/extra only apply within this scope
```

## Best Practices

1. Set user context on login, clear on logout
2. Add relevant business context (order ID, tenant, etc.)
3. Use tags for filterable, indexable data
4. Use extra for detailed debugging data
5. Implement error boundaries at feature boundaries
6. Use fingerprinting to group related errors
7. Add breadcrumbs for critical user actions

Overview

This skill provides patterns and helpers for capturing errors to Sentry, enriching events with user and business context, and grouping related failures. It describes manual and automated capture methods, React error boundaries, breadcrumb management, scopes, and fingerprinting for custom grouping. The guidance focuses on practical instrumentation so teams can triage issues faster.

How this skill works

It shows how to call Sentry.captureException and Sentry.captureMessage with tags, extras, and levels to record meaningful events. The skill explains setting global or per-event user, tag, extra, and context data, adding manual breadcrumbs, and configuring beforeBreadcrumb and beforeSend hooks. It covers Sentry.ErrorBoundary for React components, Sentry.withScope and configureScope for isolated context, and fingerprinting to control grouping behavior.

When to use it

  • Instrument try/catch blocks when exceptions are possible
  • Add business context (order IDs, tenant) before capturing errors
  • Attach user context after login and clear on logout
  • Wrap component trees with ErrorBoundary to catch rendering errors
  • Use beforeSend to filter or normalize events before they are sent

Best practices

  • Set user context on authentication and clear it on logout
  • Use tags for filterable fields and extra for verbose debugging payloads
  • Add breadcrumbs for key user actions and critical lifecycle events
  • Use isolated scopes (withScope) for per-event metadata to avoid global leakage
  • Employ fingerprinting to group related errors and reduce noise

Example use cases

  • Capture a payment failure with tags: section=checkout and extra: orderId, cart size
  • Wrap the checkout feature with Sentry.ErrorBoundary to surface UI crashes and offer retry
  • Add a breadcrumb when a user starts checkout, then capture any downstream exception with that trail
  • Use configureScope to set tenant and plan for multi-tenant filtering on Sentry
  • Normalize ChunkLoadError in beforeSend and set a custom fingerprint to consolidate noisy client bundle issues

FAQ

When should I use tags vs extra?

Use tags for small, indexed values you want to filter and group by; use extra for larger or structured debug data not suited for indexing.

How do I avoid leaking sensitive data?

Filter or remove PII in beforeSend and beforeBreadcrumb, and only set user fields you need. Avoid recording full tokens or passwords.

When to use withScope vs configureScope?

Use withScope for temporary, per-event metadata so it is discarded after capture; use configureScope to set global context that should apply to subsequent events.