unit test fixes + error refactor
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 10m52s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 10m52s
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user