Handle errors like a pro. The Vizra ADK provides a robust error handling system with specialized exceptions to help you build resilient AI agents that gracefully handle the unexpected.
Thrown when attempting to access or execute an agent that doesn’t exist in the registry.
AgentRegistry.php
use Vizra\VizraADK\Exceptions\AgentNotFoundException;// Example from AgentRegistry::getAgent()if (!isset($this->registeredAgents[$name])) { throw new AgentNotFoundException("Agent '{$name}' is not registered.");}
Thrown when there are issues with agent configuration, such as invalid settings or missing required parameters.
Configuration Validation
use Vizra\VizraADK\Exceptions\AgentConfigurationException;// Example usageif (!class_exists($config)) { throw new AgentConfigurationException( "Agent class '{$config}' does not exist." );}
Services like AgentRegistry validate configuration and throw appropriate exceptions:
AgentRegistry.php
public function getAgent(string $name): BaseAgent{ if (!isset($this->registeredAgents[$name])) { throw new AgentNotFoundException("Agent '{$name}' is not registered."); } $config = $this->registeredAgents[$name]; if (is_string($config)) { if (!class_exists($config)) { throw new AgentConfigurationException( "Agent class '{$config}' does not exist." ); } $agent = new $config(); if (!$agent instanceof BaseAgent) { throw new AgentConfigurationException( "Agent class '{$config}' must extend BaseAgent." ); } return $agent; } // Handle array configuration...}
Always throw the most specific exception type for the error scenario:
Exception Best Practices
// Good - specific exceptionif (!$agent) { throw new AgentNotFoundException("Agent 'chatbot' not found");}// Avoid - generic exceptionif (!$agent) { throw new \Exception("Agent not found");}
Let exceptions bubble up to where they can be handled appropriately:
Error Handling Layers
// In a tool - let exception bubble uppublic function execute(array $arguments, AgentContext $context): string{ if (!isset($arguments['location'])) { throw new ToolExecutionException("Location parameter is required"); } // Tool logic...}// In the controller - handle and return appropriate responsetry { $result = $agent->run($input);} catch (ToolExecutionException $e) { return response()->json(['error' => $e->getMessage()], 400);}
You can create custom exceptions for your specific use cases:
app/Exceptions/ApiRateLimitException.php
<?phpnamespace App\Exceptions;use Vizra\VizraADK\Exceptions\ToolExecutionException;class ApiRateLimitException extends ToolExecutionException{ protected string $service; protected int $retryAfter; public function __construct(string $service, int $retryAfter) { $this->service = $service; $this->retryAfter = $retryAfter; parent::__construct( "Rate limit exceeded for {$service}. Retry after {$retryAfter} seconds." ); } public function getRetryAfter(): int { return $this->retryAfter; }}