unit test fixes + error refactor
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 10m52s

This commit is contained in:
2025-12-11 18:23:59 -08:00
parent 5f1901b93d
commit 0bc65574c2
22 changed files with 112 additions and 676 deletions

View File

@@ -1,6 +1,6 @@
// src/middleware/errorHandler.ts
import { Request, Response, NextFunction } from 'express';
import { DatabaseError, UniqueConstraintError, ForeignKeyConstraintError } from '../services/db/errors.db';
import { DatabaseError, UniqueConstraintError, ForeignKeyConstraintError, NotFoundError, ValidationError } from '../services/db/errors.db';
import crypto from 'crypto';
import { logger } from '../services/logger.server';
@@ -19,15 +19,9 @@ export const errorHandler = (err: HttpError, req: Request, res: Response, next:
let message = err.message;
// --- Handle Specific Custom Error Types ---
if (err instanceof UniqueConstraintError) {
statusCode = 409; // Conflict
message = err.message || 'The record already exists.';
} else if (err instanceof ForeignKeyConstraintError) {
statusCode = 400; // Bad Request
message = err.message || 'A related record was not found.';
} else if (err instanceof DatabaseError) {
// For other generic database errors, ensure a 500 status
statusCode = 500;
// All our custom errors inherit from DatabaseError and have a status property.
if (err instanceof DatabaseError) {
statusCode = err.status || 500;
}
// --- TEST ENVIRONMENT DEBUGGING ---
@@ -46,6 +40,14 @@ export const errorHandler = (err: HttpError, req: Request, res: Response, next:
method: req.method,
body: req.body,
});
} else {
// For 4xx errors, log at a lower level (e.g., 'warn') to avoid flooding error trackers.
logger.warn(`Client Error: ${statusCode} on ${req.method} ${req.path}`, {
errorMessage: message,
path: req.path,
method: req.method,
ip: req.ip,
});
}
// In production, send a generic message for 5xx errors.