Error Handling
Handle SDK errors gracefully in your application
Error Types
The SDK provides specialized error classes for different failure scenarios:
TypeScript
import { LaikaTest, ValidationError, AuthenticationError, NetworkError, LaikaServiceError} from '@laikatest/js-client';try { const prompt = await client.getPrompt('my-prompt');} catch (error) { if (error instanceof AuthenticationError) { console.error('Invalid API key'); } else if (error instanceof NetworkError) { console.error('Network request failed'); } else if (error instanceof LaikaServiceError) { console.error('Service error:', error.message); }}Error Classes
ValidationError
Thrown when input parameters are invalid or missing required fields.
- Missing required parameters
- Invalid parameter types
- Empty or malformed values
AuthenticationError
Thrown when API authentication fails.
- Invalid or expired API key
- Missing API key
- Insufficient permissions
NetworkError
Thrown when network requests fail.
- Connection timeouts
- DNS resolution failures
- Network unavailable
LaikaServiceError
Thrown when the LaikaTest API returns an error response.
- Server-side errors (5xx)
- Resource not found (404)
- Rate limiting (429)
Best Practices
Always Use Try-Catch
TypeScript
async function fetchPrompt(name: string) { try { const prompt = await client.getPrompt(name); return prompt.compile({ name: 'User' }); } catch (error) { if (error instanceof AuthenticationError) { // Handle auth errors - maybe refresh token throw new Error('Authentication failed'); } if (error instanceof NetworkError) { // Handle network errors - maybe retry console.warn('Network error, using fallback'); return 'Default prompt content'; } // Re-throw unexpected errors throw error; }}Implement Fallbacks
TypeScript
const FALLBACK_PROMPT = 'Hello! How can I help you today?';async function getGreeting(userId: string) { try { const prompt = await client.getPrompt('greeting'); return prompt.compile({ userId }); } catch (error) { console.error('Failed to fetch prompt:', error); return FALLBACK_PROMPT; }}Log Errors for Debugging
TypeScript
try { const prompt = await client.getPrompt('my-prompt');} catch (error) { // Log full error details for debugging console.error({ name: error.name, message: error.message, stack: error.stack, timestamp: new Date().toISOString() }); // Show user-friendly message showToast('Something went wrong. Please try again.');}Tip: Consider using error monitoring services like Sentry to track production errors and get alerts when issues occur.
Next Steps
- Core Methods - Learn about SDK methods
- Quick Start - Get started with LaikaTest