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