Prompt
Always implement graceful error handling for resource lookups, data parsing, and system operations. Catch specific exceptions, provide clear error messages, and ensure the system can continue operating in a degraded state rather than failing completely.
Key practices:
- Wrap resource lookups in try/catch blocks
- Handle parsing errors explicitly
- Provide fallback behavior
- Use clear error messages
Example:
try {
$data = json_decode($input, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception(Exception::GENERAL_BAD_REQUEST, 'Invalid JSON input');
}
$document = Authorization::skip(
fn() => $dbForProject->getDocument('collection', $id)
);
if ($document->isEmpty()) {
// Graceful degradation - use empty document
$document = new Document();
Console::warning("Document {$id} not found - using empty document");
}
} catch (Throwable $e) {
// Log error for debugging
Console::error("Failed to process document {$id}: {$e->getMessage()}");
// Return safe fallback state
return new Document();
}
This approach ensures that:
- Resource lookups don’t crash the application
- Data parsing errors are caught early with clear messages
- The system can continue operating even when components fail
- Errors are logged for debugging while maintaining stability