AgentSkillsCN

sentry

使用 Sentry 进行错误跟踪和监控。 适用场景:设置错误跟踪、错误边界、报警。 不适用于:没有 Sentry 的一般错误处理模式。 工作流程:核心功能完成后设置。

SKILL.md
--- frontmatter
name: sentry
description: |
  Error tracking and monitoring with Sentry.
  Use when: setting up error tracking, error boundaries, alerting.
  Do not use for: general error handling patterns without Sentry.
  Workflow: Set up after core features are built.

Sentry Error Tracking

For latest Sentry APIs, use context7.

Setup: Use npx @sentry/wizard@latest -i nextjs or npx @sentry/wizard@latest -i reactNative


Error Handling Layers

code
┌─────────────────────────────────────────────┐
│  Layer 1: UI Recovery                       │
│  → error.tsx (Next.js) / ErrorBoundary      │
│  → User-facing fallback + reset             │
└─────────────────────────────────────────────┘
                      +
┌─────────────────────────────────────────────┐
│  Layer 2: Error Capture                     │
│  → Sentry.captureException                  │
│  → Stack traces, context, alerting          │
└─────────────────────────────────────────────┘

Rule: Always have both layers. UI recovery for users, Sentry for developers.


When to Use What

ScenarioSolution
Route-level error recoveryerror.tsx (Next.js)
Root app crashglobal-error.tsx (Next.js)
Isolate component failuresSentry.ErrorBoundary
Manual error captureSentry.captureException

Pattern: error.tsx + Sentry

tsx
// app/dashboard/error.tsx
'use client';

import * as Sentry from "@sentry/nextjs";
import { useEffect } from "react";

export default function Error({ error, reset }) {
  useEffect(() => {
    Sentry.captureException(error, {
      tags: { route: 'dashboard' },
    });
  }, [error]);

  return (
    <div>
      <h2>Something went wrong</h2>
      <button onClick={reset}>Try again</button>
    </div>
  );
}

Pattern: Component Isolation

tsx
// Isolate failures - one widget crash doesn't break the page
<Sentry.ErrorBoundary fallback={<ChartError />}>
  <AnalyticsChart />
</Sentry.ErrorBoundary>

<Sentry.ErrorBoundary fallback={<TableError />}>
  <DataTable />
</Sentry.ErrorBoundary>

Rule: Wrap independent widgets in separate ErrorBoundaries.


Context and Tags

typescript
// User context (after login)
Sentry.setUser({ id: user.id, email: user.email });

// Tags (filterable in dashboard)
Sentry.captureException(error, {
  tags: { component: 'PaymentForm', flow: 'checkout' },
  extra: { payload: requestData },
});

Rule: Always set user context after auth. Add tags for filtering.


Sample Rates

TypeDevProdWhy
Errors (sampleRate)1.01.0Capture all errors
Traces (tracesSampleRate)1.00.1Performance cost
Replays (replaysOnErrorSampleRate)01.0Debug production issues

Quick Checklist

  • SDK installed via wizard
  • DSN and environment configured
  • error.tsx at route levels (Next.js)
  • Sentry.ErrorBoundary for widget isolation
  • User context set after auth
  • Tags added for filtering
  • Source maps configured
  • Alerts set up in Sentry dashboard