Examples
Example: Implement Centralized Error Handler
Request: "Create a reusable error handling module for our Node.js API"
Result: Created lib/error-handler.ts with captureError() function wrapping Sentry.withScope(), typed severity levels mapped to Sentry levels, and tag/context/user/fingerprint support.
Example: Add Breadcrumb Trail for Checkout Flow
Request: "Add debugging context for our checkout pipeline"
Result: Added structured breadcrumb helpers (breadcrumb.auth(), breadcrumb.db(), breadcrumb.http(), breadcrumb.business()) providing a clear timeline leading up to any error in the checkout flow.
Example: Filter Noise with beforeSend
Request: "We're getting flooded with ResizeObserver and network errors"
Result: Configured beforeSend to drop ResizeObserver loop and Network request failed errors, plus scrub PII from user context and cookies.
Example: Fix Incorrect Issue Grouping
Request: "Payment timeouts from different gateways are being grouped separately"
Result: Added scope.setFingerprint(['payment-gateway-timeout', gatewayName]) to group all payment timeouts by gateway rather than by stack trace.
Python Context Manager Pattern
import sentry_sdk
from contextlib import contextmanager
@contextmanager
def sentry_scope(tags=None, context=None, user=None):
"""Reusable scoped context for Sentry events."""
with sentry_sdk.new_scope() as scope:
if tags:
for key, value in tags.items():
scope.set_tag(key, value)
if context:
for ctx_name, ctx_data in context.items():
scope.set_context(ctx_name, ctx_data)
if user:
scope.set_user(user)
yield scope
# Usage
with sentry_scope(
tags={"operation": "sync", "source": "salesforce"},
context={"sync": {"record_count": 500, "batch_id": batch_id}},
):
perform_sync_operation()TypeScript Async Error Wrapper
import * as Sentry from '@sentry/node';
export function withSentry<T>(
fn: () => Promise<T>,
context?: Record<string, unknown>,
): Promise<T> {
return fn().catch((error) => {
Sentry.withScope((scope) => {
if (context) scope.setContext('operation', context);
Sentry.captureException(error);
});
throw error;
});
}
// Usage
const user = await withSentry(
() => fetchUserData(userId),
{ userId, operation: 'fetchUser' },
);