Server Error Handling

The Aerostack Server SDK throws structured errors to help you debug quickly and recover gracefully.

Error Types

All errors extend ServerError.

Error ClassCode ExamplesDescription
DatabaseErrorDB_TABLE_NOT_FOUND, DB_CONNECTION_FAILEDIssues with D1 or Postgres queries.
CacheErrorCACHE_NOT_CONFIGURED, CACHE_GET_FAILEDProblems accessing KV store.
QueueErrorQUEUE_ENQUEUE_FAILEDFailures sending background jobs.
StorageErrorSTORAGE_UPLOAD_FAILEDR2 upload/download errors.
AIErrorAI_REQUEST_FAILEDCloudflare AI model errors.
ServiceErrorSERVICE_NOT_FOUNDIssues invoking other services.

Handling Errors

Each error includes details with a helpful suggestion and sometimes a recoveryAction.

import { DatabaseError } from '@aerostack/sdk';
 
try {
  await sdk.db.query('SELECT * FROM users');
} catch (error) {
  if (error instanceof DatabaseError) {
    console.error(`DB Error (${error.code}):`, error.message);
    
    // Check for specific suggestions
    if (error.details?.suggestion) {
      console.log('Fix:', error.details.suggestion);
      // e.g., "Run migrations first: aerostack db migrate apply"
    }
  } else {
    // Other errors
    console.error('Unknown error:', error);
  }
}

Always wrap SDK calls in try/catch blocks, especially for network-bound operations like Database, AI, and Services.