Skip to main content
Proper error handling ensures you capture actionable information while avoiding noise and maintaining application stability.

Capturing Errors

Automatic Error Capture

Sentry automatically captures unhandled errors and promise rejections:

Manual Error Capture

Capture errors in try-catch blocks:
Always capture errors before rethrowing them to ensure they’re sent to Sentry even if caught by another handler.

Capturing Messages

For non-error events, use captureMessage:

Adding Context

User Context

Identify users to track who experiences errors:
Be mindful of privacy regulations (GDPR, CCPA) when setting user data. Only include PII if you have user consent.

Tags

Add searchable key-value pairs:

Extra Context

Add structured data to events:

Fingerprinting

Control how errors are grouped:
Breadcrumbs provide a trail of events leading to an error:

Scope Management

Isolation Scope

Use isolation scopes for request-level context in Node.js:

Current Scope

Modify the current scope temporarily:

Error Boundaries (React)

Use error boundaries to catch React component errors:

Customizing Error Boundaries

Async Error Handling

Promises

Async/Await

Node.js Error Handling

Express Error Handler

Unhandled Rejections

Enriching Error Data

Stack Traces

Source Maps

Ensure source maps are uploaded for readable stack traces:

Context Lines Integration

Handling Specific Error Types

Network Errors

Validation Errors

Best Practices

Context helps debug issues faster:
Set appropriate severity levels:
Always log or capture errors:
Group related errors together:
Use Sentry’s alerts to monitor error rate increases, which can indicate new issues.

Complete Example

Next Steps

Performance Monitoring

Monitor application performance

Source Maps

Configure source maps for readable errors