domains / / getsentry/sentry-php
Propagate errors with context
Always propagate errors appropriately by rethrowing caught exceptions and maintaining error context. Catch exceptions only when you can handle them meaningfully, and ensure errors aren't silently swallowed.
Always propagate errors appropriately by rethrowing caught exceptions and maintaining error context. Catch exceptions only when you can handle them meaningfully, and ensure errors aren’t silently swallowed.
Key principles:
- Rethrow caught exceptions when you can’t handle them
- Catch \Throwable instead of \Exception for comprehensive error handling
- Keep exception handling scope narrow and focused
- Avoid generic catch blocks that mask the original error
Example:
// Bad - Swallowing the error
try {
$result = $callback();
} catch (\Throwable $e) {
$status = CheckInStatus::error();
}
// Good - Proper error propagation
try {
$result = $callback();
} catch (\Throwable $e) {
$status = CheckInStatus::error();
throw $e; // Rethrow to maintain error context
}
// Good - Focused exception handling
try {
$promise->wait();
} catch (\Throwable $e) {
return null; // Explicit handling with clear intent
}