All files / api/src/utils database-error.ts

96.8% Statements 333/344
89.53% Branches 77/86
100% Functions 9/9
96.8% Lines 333/344

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508                      1x   1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     1x   1x     1x 1x     1x     1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                         1x 1x 1x 1x 1x 1x             54x 54x 207x 54x 54x 49x 49x   5x 5x 5x   5x 5x 29x 54x 54x 54x                                                                       41x 41x 41x 6x 41x   41x 41x 30x 41x 5x 41x 41x         1x   74x 74x 74x 74x 74x   74x 2x 2x     74x 54x         54x 54x 5x 5x     49x 54x 3x 3x 3x 3x     3x     46x 54x 15x 15x 15x 15x 15x 15x 15x 15x 15x     31x 31x 30x 54x 2x 2x 2x 2x 2x 2x 2x   29x 29x 27x 54x 2x 2x 2x 2x 2x 2x 2x   27x 27x 26x 54x 1x 1x 1x 1x 1x 1x 1x   54x 1x 1x 1x 1x 1x 1x   26x 1x 1x 1x 1x 1x 1x   24x 24x     18x 18x 18x               26x 26x   26x 26x 26x 26x 26x 26x 26x 26x 26x 26x     26x 5x 5x 5x     26x 5x 13x 5x 5x 5x 13x       26x 15x     15x 2x 2x   15x 15x     26x 1x 1x 1x     26x                     41x 41x   41x 41x 30x 41x 1x 41x 3x 41x 41x 3x 41x 2x 41x 2x 41x 41x         1x 6x 6x 6x 6x 6x     6x     6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x   6x 6x 4x 4x 4x   4x   2x 2x         1x 5x 5x 5x 5x 5x 5x   5x 5x   5x 5x 5x 5x 5x 5x 5x 5x     5x 5x 5x 5x 5x         1x 5x 5x  
/**
 * Database error handling utilities
 *
 * Parses PostgreSQL/PostgREST and SQLite/libSQL error responses and converts
 * them to user-friendly messages. SQLite errors are translated into the
 * PostgreSQL codes below so that both storage backends produce the same
 * messages and the same HTTP status codes.
 * PostgreSQL error codes: https://www.postgresql.org/docs/current/errcodes-appendix.html
 */
 
/** PostgreSQL error codes mapped to user-friendly messages */
const PG_ERROR_MESSAGES: Record<string, string> = {
  // Class 23 - Integrity Constraint Violation
  '23000': 'A database constraint was violated.',
  '23001': 'Cannot delete or update due to existing references.',
  '23502': 'A required field is missing.',
  '23503': 'The referenced record does not exist.',
  '23505': 'A record with this value already exists.',
  '23514': 'The value does not meet the required constraints.',
  '23P01': 'Cannot delete or update due to existing references.',
 
  // Class 22 - Data Exception
  '22000': 'Invalid data provided.',
  '22001': 'The value is too long for this field.',
  '22003': 'The number is out of range.',
  '22007': 'Invalid date or time format.',
  '22008': 'Date or time value is out of range.',
  '22012': 'Cannot divide by zero.',
  '22P02': 'Invalid data format.',
 
  // Class 42 - Syntax Error or Access Rule Violation
  '42000': 'Database operation failed.',
  '42501': 'You do not have permission to perform this action.',
  '42601': 'Invalid request syntax.',
  '42703': 'Unknown field specified.',
  '42P01': 'The requested resource does not exist.',
 
  // Class 08 - Connection Exception
  '08000': 'Unable to connect to the database.',
  '08003': 'Database connection was lost.',
  '08006': 'Database connection failed.',
 
  // Class 53 - Insufficient Resources
  '53000': 'The server is temporarily overloaded.',
  '53100': 'The server is out of disk space.',
  '53200': 'The server is out of memory.',
  '53300': 'Too many connections to the database.',
 
  // Class 40 - Transaction Rollback
  '40000': 'The operation was rolled back.',
  '40001': 'The operation conflicted with another operation. Please try again.',
  '40P01': 'A deadlock was detected. Please try again.',
};
 
/** Common constraint name patterns mapped to user-friendly messages */
const CONSTRAINT_MESSAGES: Record<string, string> = {
  // Primary key violations
  _pkey: 'A record with this ID already exists.',
 
  // Unique constraint violations
  _unique: 'This value must be unique.',
  _key: 'This value must be unique.',
 
  // Foreign key violations
  _fkey: 'The referenced record does not exist or cannot be deleted.',
 
  // Check constraint violations
  _check: 'The value does not meet the required constraints.',
 
  // Not null violations
  _not_null: 'This field is required.',
};
 
/** Entity-specific error messages for common operations */
const ENTITY_MESSAGES: Record<string, Record<string, string>> = {
  agents: {
    '23505': 'An agent with this name already exists.',
    '23503': 'Cannot delete this agent because it has associated skills.',
  },
  skills: {
    '23505': 'A skill with this name already exists for this agent.',
    '23503':
      'Cannot delete this skill because it has associated data (logs, evaluations, etc.).',
  },
  models: {
    '23505':
      'A model with this name already exists for the selected AI provider.',
    '23503':
      'Cannot delete this model because it is being used by one or more skills.',
  },
  ai_provider_api_keys: {
    '23505': 'An API key with this name already exists.',
    '23503':
      'Cannot delete this API key because it has associated models. Delete the models first.',
  },
  skill_optimization_evaluations: {
    '23505': 'This evaluation already exists for this skill.',
    '23503': 'The referenced skill or model does not exist.',
  },
  system_settings: {
    '23503': 'The selected model does not exist.',
  },
  feedback: {
    '23503': 'The referenced log does not exist.',
  },
  improved_responses: {
    '23503': 'The referenced log does not exist.',
  },
};
 
/**
 * SQLite constraint failures, keyed by the PostgreSQL code they correspond to.
 *
 * The libSQL backend reports violations in SQLite's wording -- "UNIQUE
 * constraint failed: agents.name" -- which shares no vocabulary with
 * PostgreSQL's SQLSTATE codes. Translating here rather than at each call site
 * keeps one set of messages and status codes for both backends. Without it
 * every constraint violation on libSQL falls through to a 500, because the
 * message checks further down look for PostgreSQL's lowercase phrasing
 * ("unique constraint") and SQLite shouts its own ("UNIQUE constraint").
 */
const SQLITE_CONSTRAINT_CODES: ReadonlyArray<readonly [RegExp, string]> = [
  [/UNIQUE constraint failed/i, '23505'],
  [/FOREIGN KEY constraint failed/i, '23503'],
  [/NOT NULL constraint failed/i, '23502'],
  [/CHECK constraint failed/i, '23514'],
];
 
/**
 * SQLite names the offending column as `table.column`, which is enough to
 * reuse the same entity-specific messages the PostgREST path produces. A
 * foreign key failure reports no target at all, so both stay optional.
 */
function parseSqliteError(message: string): DatabaseErrorInfo | undefined {
  const matched = SQLITE_CONSTRAINT_CODES.find(([pattern]) =>
    pattern.test(message),
  );
  if (!matched) {
    return undefined;
  }
 
  const target = message.match(
    /constraint failed:\s*([A-Za-z0-9_]+)\.([A-Za-z0-9_]+)/,
  );
 
  return parsePostgrestError({
    code: matched[1],
    table: target?.[1],
    column: target?.[2],
  });
}
 
/** Public HTTP status codes for API responses */
export type PublicStatusCode = 400 | 404 | 409 | 500;
 
export interface DatabaseErrorInfo {
  /** User-friendly error message */
  message: string;
  /** Original error code (if available) - internal use only */
  code?: string;
  /** Constraint name (if available) - internal use only */
  constraint?: string;
  /** Table name (if available) - internal use only */
  table?: string;
  /** Column name (if available) - internal use only */
  column?: string;
  /** Internal status code for logging */
  internalStatusCode: number;
  /** Public HTTP status code to return to clients */
  statusCode: PublicStatusCode;
}
 
interface PostgrestError {
  code?: string;
  message?: string;
  details?: string;
  hint?: string;
  constraint?: string;
  table?: string;
  column?: string;
}
 
/**
 * Convert internal status code to public status code
 * Maps detailed internal codes to a simplified public set
 */
function toPublicStatusCode(internalCode: number): PublicStatusCode {
  switch (internalCode) {
    case 400:
      return 400; // Bad Request (validation errors)
    case 404:
      return 404; // Not Found
    case 403:
    case 409:
      return 409; // Conflict (constraint violations, permission issues mapped to conflict)
    default:
      return 500; // Internal Server Error (connection issues, unknown errors, 503)
  }
}
 
/**
 * Parse a PostgreSQL/PostgREST error and return user-friendly information
 */
export function parseDatabaseError(error: unknown): DatabaseErrorInfo {
  // Default error info
  const defaultError: DatabaseErrorInfo = {
    message: 'An unexpected database error occurred. Please try again.',
    internalStatusCode: 500,
    statusCode: 500,
  };
 
  if (!error) {
    return defaultError;
  }
 
  // Handle Error objects
  if (error instanceof Error) {
    const errorMessage = error.message;
 
    // Checked before the PostgREST parsing below: a SQLite message carries no
    // JSON and no SQLSTATE, so it would otherwise reach the generic patterns
    // and be reported as an unexplained 500.
    const sqliteError = parseSqliteError(errorMessage);
    if (sqliteError) {
      return sqliteError;
    }
 
    // Try to extract JSON error from PostgREST response
    const jsonMatch = errorMessage.match(/\{[\s\S]*\}/);
    if (jsonMatch) {
      try {
        const pgError = JSON.parse(jsonMatch[0]) as PostgrestError;
        return parsePostgrestError(pgError);
      } catch {
        // JSON parsing failed, continue with string parsing
      }
    }
 
    // Try to extract error code from message
    const codeMatch = errorMessage.match(/\b([0-9]{5})\b/);
    if (codeMatch) {
      const code = codeMatch[1];
      const internalCode = getStatusCodeForPgCode(code);
      return {
        message: PG_ERROR_MESSAGES[code] || defaultError.message,
        code,
        internalStatusCode: internalCode,
        statusCode: toPublicStatusCode(internalCode),
      };
    }
 
    // Check for common error patterns in the message
    if (
      errorMessage.includes('duplicate key') ||
      errorMessage.includes('unique constraint')
    ) {
      return {
        message: 'A record with this value already exists.',
        code: '23505',
        internalStatusCode: 409,
        statusCode: 409,
      };
    }
 
    if (
      errorMessage.includes('foreign key constraint') ||
      errorMessage.includes('violates foreign key')
    ) {
      return {
        message: 'The referenced record does not exist or cannot be modified.',
        code: '23503',
        internalStatusCode: 409,
        statusCode: 409,
      };
    }
 
    if (
      errorMessage.includes('not-null constraint') ||
      errorMessage.includes('null value')
    ) {
      return {
        message: 'A required field is missing.',
        code: '23502',
        internalStatusCode: 400,
        statusCode: 400,
      };
    }
 
    if (errorMessage.includes('409')) {
      return {
        message: 'The operation conflicts with existing data.',
        internalStatusCode: 409,
        statusCode: 409,
      };
    }
 
    if (errorMessage.includes('404')) {
      return {
        message: 'The requested resource was not found.',
        internalStatusCode: 404,
        statusCode: 404,
      };
    }
 
    return defaultError;
  }
 
  // Handle plain objects (e.g., parsed JSON)
  if (typeof error === 'object') {
    return parsePostgrestError(error as PostgrestError);
  }
 
  return defaultError;
}
 
/**
 * Parse a PostgREST error object
 */
function parsePostgrestError(pgError: PostgrestError): DatabaseErrorInfo {
  const { code, constraint, table, column, message, details } = pgError;
 
  const internalCode = code ? getStatusCodeForPgCode(code) : 500;
  const result: DatabaseErrorInfo = {
    message: 'An unexpected database error occurred.',
    code,
    constraint,
    table,
    column,
    internalStatusCode: internalCode,
    statusCode: toPublicStatusCode(internalCode),
  };
 
  // Try entity-specific message first
  if (code && table && ENTITY_MESSAGES[table]?.[code]) {
    result.message = ENTITY_MESSAGES[table][code];
    return result;
  }
 
  // Try constraint-specific message
  if (constraint) {
    for (const [pattern, msg] of Object.entries(CONSTRAINT_MESSAGES)) {
      if (constraint.includes(pattern)) {
        result.message = msg;
        return result;
      }
    }
  }
 
  // Try error code message
  if (code && PG_ERROR_MESSAGES[code]) {
    result.message = PG_ERROR_MESSAGES[code];
 
    // Add column context if available
    if (column && code === '23502') {
      result.message = `The field "${column}" is required.`;
    }
 
    return result;
  }
 
  // Use original message if it's user-friendly enough
  if (message && !message.includes('ERROR:') && message.length < 200) {
    result.message = message;
    return result;
  }
 
  // Use details if available and user-friendly
  if (details && !details.includes('Key (') && details.length < 200) {
    result.message = details;
    return result;
  }
 
  return result;
}
 
/**
 * Get appropriate HTTP status code for a PostgreSQL error code
 */
function getStatusCodeForPgCode(code: string): number {
  const codeClass = code.substring(0, 2);
 
  switch (codeClass) {
    case '23': // Integrity Constraint Violation
      return code === '23502' ? 400 : 409; // Not null -> 400, others -> 409
    case '22': // Data Exception
      return 400;
    case '42': // Syntax Error or Access Rule Violation
      return code === '42501' ? 403 : 400;
    case '08': // Connection Exception
    case '53': // Insufficient Resources
      return 503;
    case '40': // Transaction Rollback
      return 409;
    default:
      return 500;
  }
}
 
/**
 * Get a user-friendly error message for a specific entity and operation
 */
export function getEntityErrorMessage(
  entity: string,
  operation: 'create' | 'update' | 'delete',
  error: unknown,
): string {
  const errorInfo = parseDatabaseError(error);
 
  // Entity-specific operation messages
  const operationMessages: Record<
    string,
    Record<string, Record<string, string>>
  > = {
    agents: {
      create: {
        '23505': 'An agent with this name already exists.',
        default: 'Failed to create agent. Please try again.',
      },
      delete: {
        '23503':
          'Cannot delete this agent because it has skills. Delete the skills first.',
        default: 'Failed to delete agent. Please try again.',
      },
    },
    skills: {
      create: {
        '23505': 'A skill with this name already exists for this agent.',
        default: 'Failed to create skill. Please try again.',
      },
      delete: {
        '23503':
          'Cannot delete this skill because it has associated data. Try archiving it instead.',
        default: 'Failed to delete skill. Please try again.',
      },
    },
    models: {
      create: {
        '23505':
          'A model with this name already exists for the selected provider.',
        default: 'Failed to create model. Please try again.',
      },
      delete: {
        '23503':
          'Cannot delete this model because it is used by skills. Remove it from all skills first.',
        default: 'Failed to delete model. Please try again.',
      },
    },
    ai_providers: {
      create: {
        '23505': 'An API key with this name already exists.',
        default: 'Failed to create API key. Please try again.',
      },
      delete: {
        '23503':
          'Cannot delete this API key because it has models. Delete the models first.',
        default: 'Failed to delete API key. Please try again.',
      },
    },
  };
 
  const entityMessages = operationMessages[entity]?.[operation];
  if (entityMessages) {
    if (errorInfo.code && entityMessages[errorInfo.code]) {
      return entityMessages[errorInfo.code];
    }
    return entityMessages.default || errorInfo.message;
  }
 
  return errorInfo.message;
}
 
/**
 * Custom error class for database errors with user-friendly messages
 */
export class DatabaseError extends Error {
  public readonly code?: string;
  public readonly constraint?: string;
  public readonly table?: string;
  public readonly column?: string;
  public readonly statusCode: PublicStatusCode;
  public readonly userMessage: string;
 
  constructor(error: unknown, entity?: string) {
    const errorInfo = parseDatabaseError(error);
 
    super(errorInfo.message);
    this.name = 'DatabaseError';
    this.code = errorInfo.code;
    this.constraint = errorInfo.constraint;
    this.table = entity || errorInfo.table;
    this.column = errorInfo.column;
    this.statusCode = errorInfo.statusCode;
    this.userMessage = errorInfo.message;
 
    // Maintain proper stack trace
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, DatabaseError);
    }
  }
}
 
/**
 * Check if an error is a database error
 */
export function isDatabaseError(error: unknown): error is DatabaseError {
  return error instanceof DatabaseError;
}