Error Handling and Logging - pacificnm/wiki-ai GitHub Wiki
Error Handling and Logging System
This document describes the comprehensive error handling and logging system implemented in the wiki-ai project.
Overview
The error handling system consists of several components working together to provide robust error management for both frontend and backend:
- Backend Logger: Winston-based logging system with multiple transports
- Backend Error Handling: Custom error classes and middleware for consistent error responses
- Frontend Error Boundary: React Error Boundary for catching and displaying user-friendly errors
- Frontend Error Hooks: Custom React hooks for handling API errors and user notifications
- Error Reporting: Client-side error logging to server endpoint
Backend Components
server/middleware/logger.js
)
1. Logger Middleware (Winston-based logger with the following features:
- Console and file logging
- Configurable log levels
- Request-scoped logging
- HTTP request logging via Morgan
- Structured logging format
Usage:
import { logger } from './middleware/logger.js';
logger.info('Operation completed');
logger.error('Operation failed', { error: error.message });
logger.warn('Warning message', { userId: 'user123' });
server/middleware/error.js
)
2. Error Handling Middleware (Comprehensive error handling system with:
- Custom error classes (
AppError
,ValidationError
,AuthenticationError
, etc.) - Global error handler middleware
- Async error wrapper
- Development vs production error responses
Custom Error Classes:
AppError
: Base application error classValidationError
: Data validation errorsAuthenticationError
: Authentication failuresAuthorizationError
: Permission errorsNotFoundError
: Resource not foundConflictError
: Data conflictsRateLimitError
: Rate limiting errors
Usage:
import { AppError, asyncHandler } from './middleware/error.js';
// Throw custom error
throw new AppError('User not found', 404, 'NOT_FOUND');
// Wrap async route handlers
router.get('/users', asyncHandler(async (req, res) => {
const users = await User.find();
res.json(users);
}));
server/controllers/errorController.js
)
3. Error Controller (Handles client-side error reporting with:
- Error validation using Zod schemas
- Structured error logging
- Error statistics (for admin users)
- Error log cleanup utilities
API Endpoints:
POST /api/errors
- Log client-side errorsGET /api/errors/stats
- Get error statistics (admin)DELETE /api/errors/cleanup
- Clean old error logs (admin)GET /api/errors/test
- Test error scenarios (development only)
server/routes/errorRoutes.js
)
4. Error Routes (Express routes for error handling endpoints with proper authentication and validation.
Frontend Components
client/components/ErrorBoundary.jsx
)
1. Error Boundary (React Error Boundary component with:
- Catches JavaScript errors in component tree
- Displays user-friendly error messages
- Categorizes errors (network, rendering, data, unknown)
- Logs errors to server endpoint
- Retry mechanisms
- Material-UI design integration
Usage:
import ErrorBoundary from './components/ErrorBoundary';
function App() {
return (
<ErrorBoundary>
<YourAppComponents />
</ErrorBoundary>
);
}
client/hooks/useError.js
)
2. Error Handling Hooks (Custom React hooks for error management:
useError()
Hook:
- Display error messages to users via Snackbar
- Handle different error types
- Clear errors
- Async operation wrapper
useApiError()
Hook:
- Handle API response errors
- Parse HTTP status codes
- Convert technical errors to user-friendly messages
Usage:
import { useError, useApiError } from './hooks/useError';
function MyComponent() {
const { showError, showSuccess } = useError();
const { handleApiError } = useApiError();
const handleSubmit = async () => {
try {
const result = await api.submitData();
showSuccess('Data submitted successfully!');
} catch (error) {
handleApiError(error, 'Submit data');
}
};
}
Error Reporting Flow
Client-Side Error Reporting
- JavaScript Errors: Caught by
useError
hook and logged to server - React Errors: Caught by
ErrorBoundary
component and logged to server - API Errors: Handled by
useApiError
hook with user notifications
Server-Side Error Logging
- Error Validation: Incoming errors validated with Zod schemas
- Structured Logging: Errors logged with Winston in structured format
- Error Storage: Logged to files and console (can be extended to databases)
- Error Monitoring: Ready for integration with monitoring services
Configuration
Environment Variables
# Logging
LOG_LEVEL=info
LOG_DIR=./logs
# Error Handling
NODE_ENV=development
Log Files
Logs are stored in the following files:
logs/error.log
- Error-level logs onlylogs/combined.log
- All log levels- Console output for development
Integration with Monitoring Services
The system is designed to integrate with external monitoring services:
Supported Services
- Sentry: For error tracking and performance monitoring
- LogRocket: For session replay and error tracking
- Bugsnag: For error monitoring
- Datadog: For application monitoring
- New Relic: For application performance monitoring
Integration Points
- Client-side: Update
sendToErrorService
inuseError.js
- Server-side: Add transport to Winston logger configuration
- Error Controller: Extend to send to external APIs
Best Practices
Frontend
- Wrap Components: Always wrap your app with
ErrorBoundary
- Use Hooks: Use
useError
anduseApiError
hooks for consistent error handling - User-Friendly Messages: Convert technical errors to user-friendly messages
- Don't Spam: Avoid showing multiple error messages for the same issue
Backend
- Use Custom Errors: Use custom error classes instead of generic Error
- Async Wrappers: Always wrap async route handlers with
asyncHandler
- Structured Logging: Use structured logging with relevant context
- Error Codes: Use consistent error codes for client-side handling
General
- Log Context: Always include relevant context in error logs
- Rate Limiting: Implement rate limiting for error reporting endpoints
- Privacy: Don't log sensitive information (passwords, tokens)
- Monitoring: Set up alerts for error rate thresholds
Testing Error Handling
Development Testing
Use the test endpoint to simulate different error types:
# Test different error types
GET /api/errors/test?type=error
GET /api/errors/test?type=validation
GET /api/errors/test?type=auth
GET /api/errors/test?type=server
Frontend Testing
// Test Error Boundary
const TestError = () => {
throw new Error('Test error for boundary');
};
// Test useError hook
const { showError } = useError();
showError('Test error message');
Troubleshooting
Common Issues
- Logs not appearing: Check LOG_LEVEL environment variable
- Client errors not reaching server: Check network and authentication
- Error boundary not catching: Ensure it wraps the component tree
- Infinite error loops: Check error logging doesn't cause more errors
Debug Mode
Set NODE_ENV=development
for:
- Detailed error messages
- Stack traces in responses
- Additional logging
- Test endpoints enabled
Future Enhancements
- Error Aggregation: Implement error aggregation to prevent spam
- User Feedback: Add user feedback collection on errors
- Error Analytics: Implement error analytics dashboard
- Performance Monitoring: Add performance error detection
- Mobile Support: Extend error handling for mobile applications