@agentdb/sdk
Version:
JavaScript SDK for AgentDB database service
133 lines (116 loc) • 4.34 kB
JavaScript
/**
* Error Handling Example
*
* This example demonstrates proper error handling with the AgentDB SDK,
* showing how to catch and handle different types of errors.
*/
import {
DatabaseService,
AgentDBError,
AuthenticationError,
ValidationError,
DatabaseError
} from '../index.js';
async function errorHandlingExample() {
const BASE_URL = 'https://api.agentdb.dev';
const API_KEY = 'your-api-key-here';
const TOKEN = 'your-uuid-token-here';
console.log('=== Error Handling Examples ===\n');
// Example 1: Invalid API key
console.log('1. Testing invalid API key...');
try {
const invalidService = new DatabaseService(BASE_URL, 'invalid-api-key');
await invalidService.listDatabases(TOKEN);
} catch (error) {
if (error instanceof AuthenticationError) {
console.log('✓ Caught AuthenticationError:', error.message);
} else {
console.log('✗ Unexpected error type:', error.constructor.name);
}
}
// Example 2: Missing required parameters
console.log('\n2. Testing validation errors...');
try {
const service = new DatabaseService(BASE_URL, API_KEY);
await service.listDatabases(''); // Empty token
} catch (error) {
if (error instanceof ValidationError) {
console.log('✓ Caught ValidationError:', error.message);
} else {
console.log('✗ Unexpected error type:', error.constructor.name);
}
}
// Example 3: Invalid SQL
console.log('\n3. Testing database errors...');
try {
const service = new DatabaseService(BASE_URL, API_KEY);
const connection = service.connect(TOKEN, 'test-db', 'sqlite');
await connection.execute({
sql: 'INVALID SQL STATEMENT',
params: []
});
} catch (error) {
if (error instanceof DatabaseError) {
console.log('✓ Caught DatabaseError:', error.message);
} else if (error instanceof AgentDBError) {
console.log('✓ Caught AgentDBError:', error.message);
} else {
console.log('✗ Unexpected error type:', error.constructor.name);
}
}
// Example 4: Network errors
console.log('\n4. Testing network errors...');
try {
const invalidService = new DatabaseService('https://invalid-domain-that-does-not-exist.com', API_KEY);
await invalidService.listDatabases(TOKEN);
} catch (error) {
if (error instanceof AgentDBError && error.message.includes('Network error')) {
console.log('✓ Caught Network error:', error.message);
} else {
console.log('✗ Unexpected error type:', error.constructor.name, error.message);
}
}
// Example 5: Comprehensive error handling function
console.log('\n5. Comprehensive error handling...');
async function safeExecute(connection, statements) {
try {
const result = await connection.execute(statements);
console.log('✓ Execution successful');
return result;
} catch (error) {
console.log('✗ Execution failed');
if (error instanceof AuthenticationError) {
console.log(' → Authentication issue. Check your API key.');
console.log(' → Error:', error.message);
} else if (error instanceof ValidationError) {
console.log(' → Validation issue. Check your parameters.');
console.log(' → Error:', error.message);
} else if (error instanceof DatabaseError) {
console.log(' → Database issue. Check your SQL syntax.');
console.log(' → Error:', error.message);
} else if (error instanceof AgentDBError) {
console.log(' → AgentDB service issue.');
console.log(' → Error:', error.message);
console.log(' → Status code:', error.statusCode);
} else {
console.log(' → Unexpected error.');
console.log(' → Error:', error.message);
}
throw error; // Re-throw if you want calling code to handle it
}
}
// Test the comprehensive error handler
try {
const service = new DatabaseService(BASE_URL, API_KEY);
const connection = service.connect(TOKEN, 'test-db', 'sqlite');
await safeExecute(connection, {
sql: 'SELECT 1 as test',
params: []
});
} catch (error) {
// Error already logged by safeExecute
}
console.log('\n=== Error Handling Examples Complete ===');
}
// Run the example
errorHandlingExample();