Error Handling and Logging
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The TAMV Online system implements a centralized error handling and logging strategy designed to maintain system stability and provide clear diagnostic information during runtime failures. This system captures errors across the stack—from database connection issues in the backend to operational failures in API endpoints—ensuring that the "most ambitious system" remains maintainable and robust.
The primary objective is to intercept exceptions, log them with appropriate context, and return standardized responses to the client, preventing the exposure of sensitive stack traces in production environments.
Sources: README.md:1-2, backend/middleware/errorMiddleware.js:1-15
Backend Error Handling Architecture
The backend utilizes a middleware-based approach to catch errors globally. This prevents the application from crashing and ensures that every failed request receives a structured JSON response.
Global Error Middleware
The system employs a custom error handler that overrides the default Express error behavior. It distinguishes between standard errors and "404 Not Found" scenarios.
The diagram illustrates the flow of a failed request through the global error middleware.
Sources: backend/middleware/errorMiddleware.js:1-20, backend/server.js:25-30
Database Connection Logging
Error handling begins at the infrastructure level, specifically during the initialization of the MongoDB connection. If the connection fails, the system logs the error to the console and exits the process to prevent the application from running in an unstable state.
| Event | Action | Source |
|---|---|---|
| DB Connection Success | Log host name to console | db.js |
| DB Connection Failure | Log error and exit process(1) | db.js |
Sources: backend/config/db.js:8-15
Logging Mechanisms
Logging in TAMV Online is primarily focused on console output for real-time monitoring of server status and error diagnostics.
Server Lifecycle Logging
The system logs critical lifecycle events to ensure developers can track the state of the application:
- Environment Status: Logs the port and mode (development/production) upon startup.
- Database Status: Logs successful connections to the MongoDB cluster.
- Request Errors: Logs stack traces and status codes for failed API calls when in development mode.
Sequence of events during system initialization and logging of connectivity status.
Sources: backend/config/db.js:12-14, backend/server.js:32-35
Frontend Error Boundaries
The frontend application, built with React, manages operational errors through dependency management and specific script definitions. While the backend handles data-related errors, the frontend focuses on build-time and runtime stability through its configuration.
Dependency Management
The package.json file defines the environment where logging and error reporting tools are integrated (e.g., React scripts).
| Module | Purpose | Source |
|---|---|---|
react-scripts | Handles build errors and development logging | package.json |
axios | Used for capturing HTTP error responses from the backend | package.json |
Sources: frontend/package.json:10-15
Standardized Error Response Format
When an error is captured by the middleware, it is transformed into a consistent JSON structure. This allows the frontend to predictably parse error messages and display them to the user.
{
"message": "Error message description",
"stack": "Stack trace (only available in development mode)"
}
Sources: backend/middleware/errorMiddleware.js:10-14
Summary
Error handling and logging in TAMV Online are centralized to ensure that all failures, whether they occur during database initialization or within an API route, are captured and reported. By utilizing middleware for the backend and structured logging for system events, the project maintains a clear audit trail for debugging while protecting production data from exposure.
Sources: backend/server.js:20-35, backend/middleware/errorMiddleware.js:1-15