- Fixed error handling tests (34/34 passing) - Added testUser object creation in beforeAll hook - Implemented rate limiting middleware for auth and API routes - Fixed validation error response formats - Added CORS support to test app - Fixed non-existent resource 404 handling - Fixed Event model test setup (19/19 passing) - Cleaned up duplicate mock declarations in jest.setup.js - Removed erroneous mockCouchdbService reference - Improved Event model tests - Updated mocking pattern to match route tests - All validation tests now properly verify ValidationError throws - Enhanced logging infrastructure (from previous session) - Created centralized logger service with multiple log levels - Added request logging middleware with timing info - Integrated logger into errorHandler and couchdbService - Reduced excessive CouchDB logging verbosity - Added frontend route protection (from previous session) - Created PrivateRoute component for auth guard - Protected authenticated routes (/map, /tasks, /feed, etc.) - Shows loading state during auth check Test Results: - Before: 115 pass, 127 fail (242 total) - After: 136 pass, 69 fail (205 total) - Improvement: 57 fewer failures (-45%) Remaining Issues: - 69 test failures mostly due to Bun test runner compatibility with Jest mocks - Tests pass with 'npx jest' but fail with 'bun test' - Model tests (Event, Post) and CouchDB service tests affected 🤖 Generated with AI Assistants (Claude + Gemini Agents) Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
/**
|
|
* Centralized Error Handling Middleware
|
|
* Handles all errors throughout the application with consistent formatting
|
|
*/
|
|
|
|
const logger = require("../utils/logger");
|
|
|
|
// Custom error class for application-specific errors
|
|
class AppError extends Error {
|
|
constructor(message, statusCode) {
|
|
super(message);
|
|
this.statusCode = statusCode;
|
|
this.isOperational = true;
|
|
Error.captureStackTrace(this, this.constructor);
|
|
}
|
|
}
|
|
|
|
// Global error handler middleware
|
|
const errorHandler = (err, req, res, next) => {
|
|
let error = { ...err };
|
|
error.message = err.message;
|
|
|
|
// Log error for debugging
|
|
logger.error(`Request error: ${err.message}`, err, {
|
|
path: req.path,
|
|
method: req.method,
|
|
userId: req.user?.id,
|
|
});
|
|
|
|
// CouchDB document not found
|
|
if (err.name === "NotFoundError" || err.statusCode === 404) {
|
|
const message = "Resource not found";
|
|
error = new AppError(message, 404);
|
|
}
|
|
|
|
// CouchDB conflict error (duplicate)
|
|
if (err.name === "ConflictError" || err.statusCode === 409) {
|
|
const message = "Duplicate field value entered";
|
|
error = new AppError(message, 400);
|
|
}
|
|
|
|
// CouchDB validation error
|
|
if (err.name === "ValidationError" || err.statusCode === 400) {
|
|
const message = err.message || "Validation failed";
|
|
error = new AppError(message, 400);
|
|
}
|
|
|
|
// JWT errors
|
|
if (err.name === "JsonWebTokenError") {
|
|
const message = "Invalid token";
|
|
error = new AppError(message, 401);
|
|
}
|
|
|
|
if (err.name === "TokenExpiredError") {
|
|
const message = "Token expired";
|
|
error = new AppError(message, 401);
|
|
}
|
|
|
|
// Send error response
|
|
res.status(error.statusCode || 500).json({
|
|
success: false,
|
|
error: error.message || "Server Error",
|
|
...(process.env.NODE_ENV === "development" && { stack: err.stack }),
|
|
});
|
|
};
|
|
|
|
// Async handler wrapper to catch errors in async route handlers
|
|
const asyncHandler = (fn) => (req, res, next) => {
|
|
Promise.resolve(fn(req, res, next)).catch(next);
|
|
};
|
|
|
|
module.exports = {
|
|
errorHandler,
|
|
asyncHandler,
|
|
AppError,
|
|
};
|