Back to all reviewers

Propagate errors with context

getsentry/sentry-php
Based on 5 comments
PHP

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.

Error Handling PHP

Reviewer Prompt

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:

  1. Rethrow caught exceptions when you can’t handle them
  2. Catch \Throwable instead of \Exception for comprehensive error handling
  3. Keep exception handling scope narrow and focused
  4. 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
}
5
Comments Analyzed
PHP
Primary Language
Error Handling
Category

Source Discussions