Add comprehensive tests for hooks, middleware, and routes
Some checks are pending
Deploy to Test Environment / deploy-to-test (push) Has started running
Some checks are pending
Deploy to Test Environment / deploy-to-test (push) Has started running
- Implement tests for `useFlyers`, `useMasterItems`, `useModal`, `useUserData` hooks to ensure correct functionality and error handling. - Create tests for `fileUpload.middleware` and `validation.middleware` to validate file uploads and request data. - Add tests for `AddressForm` and `AuthView` components to verify rendering, user interactions, and API calls. - Develop tests for `deals.routes` to check authentication and response for best prices on watched items.
This commit is contained in:
42
server.ts
42
server.ts
@@ -25,6 +25,7 @@ import systemRouter from './src/routes/system.routes';
|
||||
import healthRouter from './src/routes/health.routes';
|
||||
import { errorHandler } from './src/middleware/errorHandler';
|
||||
import { backgroundJobService, startBackgroundJobs } from './src/services/backgroundJobService';
|
||||
import type { UserProfile } from './src/types';
|
||||
import { analyticsQueue, weeklyAnalyticsQueue, gracefulShutdown } from './src/services/queueService.server';
|
||||
|
||||
// --- START DEBUG LOGGING ---
|
||||
@@ -80,27 +81,44 @@ const getDurationInMilliseconds = (start: [number, number]): number => {
|
||||
};
|
||||
|
||||
const requestLogger = (req: Request, res: Response, next: NextFunction) => {
|
||||
const requestId = randomUUID();
|
||||
const user = req.user as UserProfile | undefined;
|
||||
const start = process.hrtime();
|
||||
const { method, originalUrl } = req;
|
||||
|
||||
// If the request times out, log it.
|
||||
if (req.timedout) {
|
||||
logger.error(`REQUEST TIMEOUT: ${method} ${originalUrl} exceeded the 5m limit.`);
|
||||
}
|
||||
// Create a request-scoped logger instance as per ADR-004
|
||||
// This attaches contextual info to every log message generated for this request.
|
||||
req.log = logger.child({
|
||||
request_id: requestId,
|
||||
user_id: user?.user_id, // This will be undefined until the auth middleware runs, but the logger will hold the reference.
|
||||
ip_address: req.ip,
|
||||
});
|
||||
|
||||
logger.debug(`[Request Logger] INCOMING: ${method} ${originalUrl}`);
|
||||
req.log.debug({ method, originalUrl }, `[Request Logger] INCOMING`);
|
||||
|
||||
res.on('finish', () => {
|
||||
const user = req.user as { user_id?: string } | undefined;
|
||||
const durationInMilliseconds = getDurationInMilliseconds(start);
|
||||
const { statusCode } = res;
|
||||
const userIdentifier = user?.user_id ? ` (User: ${user.user_id})` : '';
|
||||
const { statusCode, statusMessage } = res;
|
||||
const finalUser = req.user as UserProfile | undefined;
|
||||
|
||||
const logMessage = `${method} ${originalUrl} ${statusCode} ${durationInMilliseconds.toFixed(2)}ms${userIdentifier}`;
|
||||
// The base log object includes details relevant for all status codes.
|
||||
const logDetails: Record<string, any> = {
|
||||
user_id: finalUser?.user_id,
|
||||
method,
|
||||
originalUrl,
|
||||
statusCode,
|
||||
statusMessage,
|
||||
duration: durationInMilliseconds.toFixed(2),
|
||||
};
|
||||
|
||||
if (statusCode >= 500) logger.error(logMessage);
|
||||
else if (statusCode >= 400) logger.warn(logMessage);
|
||||
else logger.info(logMessage);
|
||||
// For failed requests, add the full request details for better debugging.
|
||||
// Pino's `redact` config will automatically sanitize sensitive headers and body fields.
|
||||
if (statusCode >= 400) {
|
||||
logDetails.req = { headers: req.headers, body: req.body };
|
||||
}
|
||||
if (statusCode >= 500) req.log.error(logDetails, 'Request completed with server error');
|
||||
else if (statusCode >= 400) req.log.warn(logDetails, 'Request completed with client error');
|
||||
else req.log.info(logDetails, 'Request completed successfully');
|
||||
});
|
||||
|
||||
next();
|
||||
|
||||
Reference in New Issue
Block a user