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:
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
}
Enter the URL of a public GitHub repository