Error Handling

    The SDK provides typed error classes for every API failure scenario.

    Error Hierarchy

    CadraError (base)
    ├── AuthenticationError (401)
    ├── AuthorizationError (403)
    ├── NotFoundError (404)
    ├── ValidationError (400/422)
    ├── ConflictError (409)
    ├── RateLimitError (429)
    ├── ServerError (5xx)
    └── NetworkError (0)

    Catching Errors

    typescript
    import {
      CadraError, AuthenticationError,
      RateLimitError, ValidationError, NetworkError,
    } from '@cadraos/sdk';
    
    try {
      const result = await client.agents.executeSync('agent-uuid', { task: 'Hello' });
    } catch (error) {
      if (error instanceof RateLimitError) {
        console.log(`Rate limited. Retry after ${error.retryAfter}s`);
      } else if (error instanceof AuthenticationError) {
        console.log('Invalid or expired API key');
      } else if (error instanceof ValidationError) {
        error.errors.forEach(e => console.log(`  ${e.field}: ${e.message}`));
      } else if (error instanceof NetworkError) {
        console.log('Network error — check connectivity');
      } else if (error instanceof CadraError) {
        console.log(`API error ${error.code} (${error.status}): ${error.message}`);
        console.log('Request ID:', error.requestId);
      }
    }

    In Chat UI

    tsx
    <AgentChat
      agentId="agent-uuid"
      onError={(error) => {
        // error: { code, message, details?, retryable? }
        if (error.code === 'rate_limit') {
          showToast('Too many requests. Please wait.');
        }
      }}
    />

    The chat UI automatically shows an error banner with a retry button when the error is retryable.