Skip to content

Commit c7e1382

Browse files
feat(errors): adopt DatabaseError and ValidationError across database layer
- Replace generic Error throws with DatabaseError for connection, query, transaction, and migration failures - Add Zod validation schemas for all MCP tool inputs (schemas.ts) - Update MCP server handlers to use validateInput for input validation - Remove unused imports and prefix unused parameters STA-186: Error Handling Infrastructure STA-187: Security and Validation Improvements
1 parent fefb7a9 commit c7e1382

8 files changed

Lines changed: 614 additions & 119 deletions

File tree

src/core/database/batch-operations.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55

66
import Database from 'better-sqlite3';
7-
import { getConnectionPool } from './connection-pool.js';
7+
// Connection pool imported when needed: getConnectionPool
88
import { logger } from '../monitoring/logger.js';
99
import { trace } from '../trace/index.js';
10+
import { ErrorCode, wrapError } from '../errors/index.js';
1011

1112
export interface BatchOperation {
1213
table: string;
@@ -159,9 +160,15 @@ export class BatchOperationsManager {
159160
stats.successfulInserts += result.changes;
160161
} catch (error: unknown) {
161162
stats.failedInserts++;
163+
const wrappedError = wrapError(
164+
error,
165+
'Failed to update frame digest',
166+
ErrorCode.DB_UPDATE_FAILED,
167+
{ frameId: update.frame_id }
168+
);
162169
logger.warn('Failed to update frame digest', {
163170
frameId: update.frame_id,
164-
error: (error as Error).message,
171+
error: wrappedError.message,
165172
});
166173
}
167174
}
@@ -245,9 +252,15 @@ export class BatchOperationsManager {
245252
stats.successfulInserts += result.changes;
246253
} catch (error: unknown) {
247254
stats.failedInserts++;
255+
const wrappedError = wrapError(
256+
error,
257+
`Failed to insert ${table} record`,
258+
ErrorCode.DB_INSERT_FAILED,
259+
{ table, record }
260+
);
248261
logger.warn(`Failed to insert ${table} record`, {
249262
record,
250-
error: (error as Error).message,
263+
error: wrappedError.message,
251264
});
252265
}
253266
}
@@ -315,7 +328,13 @@ export class BatchOperationsManager {
315328
}
316329
} catch (error: unknown) {
317330
stats.failedInserts += batch.length;
318-
logger.error('Batch processing failed', error as Error, {
331+
const wrappedError = wrapError(
332+
error,
333+
'Batch processing failed',
334+
ErrorCode.DB_TRANSACTION_FAILED,
335+
{ batchNumber: stats.batchesProcessed + 1, batchSize: batch.length }
336+
);
337+
logger.error('Batch processing failed', wrappedError, {
319338
batchNumber: stats.batchesProcessed + 1,
320339
batchSize: batch.length,
321340
});
@@ -358,7 +377,13 @@ export class BatchOperationsManager {
358377
tables: groupedOps.size,
359378
});
360379
} catch (error: unknown) {
361-
logger.error('Batch queue processing failed', error as Error);
380+
const wrappedError = wrapError(
381+
error,
382+
'Batch queue processing failed',
383+
ErrorCode.DB_TRANSACTION_FAILED,
384+
{ operationsCount: operations.length }
385+
);
386+
logger.error('Batch queue processing failed', wrappedError);
362387
} finally {
363388
this.isProcessing = false;
364389
}

src/core/database/connection-pool.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { Pool, PoolClient, PoolConfig } from 'pg';
77
import { EventEmitter } from 'events';
88
import { logger } from '../monitoring/logger.js';
9+
import { DatabaseError, ErrorCode } from '../errors/index.js';
910

1011
export interface ConnectionPoolConfig extends PoolConfig {
1112
// Basic pool settings
@@ -278,7 +279,12 @@ export class ConnectionPool extends EventEmitter {
278279
} catch (error: unknown) {
279280
this.metrics.totalErrors++;
280281
logger.error('Failed to acquire connection:', error);
281-
throw error;
282+
throw new DatabaseError(
283+
'Failed to acquire database connection',
284+
ErrorCode.DB_CONNECTION_FAILED,
285+
{ pool: 'paradedb' },
286+
error instanceof Error ? error : undefined
287+
);
282288
}
283289
}
284290

@@ -435,7 +441,12 @@ export class ConnectionPool extends EventEmitter {
435441
logger.error('Transaction rollback failed:', rollbackError);
436442
this.markConnectionAsBad(client);
437443
}
438-
throw error;
444+
throw new DatabaseError(
445+
'Transaction failed',
446+
ErrorCode.DB_TRANSACTION_FAILED,
447+
{ operation: 'transaction' },
448+
error instanceof Error ? error : undefined
449+
);
439450
} finally {
440451
this.release(client);
441452
}

0 commit comments

Comments
 (0)