Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup server, Enhance Request Tracking, Error Handling, and Logging #1

Merged
merged 17 commits into from
Apr 7, 2024

Conversation

foyzulkarim
Copy link
Owner

@foyzulkarim foyzulkarim commented Apr 7, 2024

Summary

We've established a modular Node.js server setup with Express.js as the web framework and Winston for logging.

Key features include a request ID middleware using AsyncLocalStorage for tracking requests across asynchronous operations, as well as centralized error handling for uncaught exceptions, unhandled rejections, and termination signals.

To promote maintainability, we employ a middleware-based architecture and hints of domain-driven design for structuring application logic.

Additionally, a custom AppError class normalizes error representation for consistent handling and potential integration with a monitoring system.

Details:

  • Request ID Tracking:

    • Implemented a middleware using AsyncLocalStorage to generate and associate unique request IDs with each incoming request.
    • Request IDs are included in response headers and propagated throughout asynchronous operations.
    • Integrated requestId into the Winston logger for seamless correlation of log entries with requests.
  • Centralized Error Handling:

    • Consolidated error handling logic for uncaught exceptions, unhandled promise rejections, and termination signals (SIGTERM, SIGINT).
    • Introduced a custom AppError class to normalize error representation.
    • Implemented graceful shutdown of the HTTP server when possible.
    • Integrated error metric firing (would send to a monitoring system in a production setting).

Files Modified:

  • start.js: Updated to configure error handling and request ID middleware.
  • server.js: Includes request-context middleware and error-handling middleware.
  • app.js: Employs error handling middleware.
  • request-context.js: New file containing the request ID middleware implementation.
  • error-handling.js: New file encapsulating the error handling logic and AppError class.
  • logger.js: Modified to incorporate the requestId into Winston log entries.
  • Domain > product api: Sample API endpoint demonstrating usage of middleware and error handling setup.

Benefits

  • Enhanced Observability: The addition of request IDs aids in debugging and tracing requests through the system, especially in distributed environments.
  • Robust Error Handling: Centralized error handling ensures consistent processing of errors, promoting application stability.
  • Improved Maintainability: Well-structured middleware and error handling implementation increase code readability and maintainability.

@foyzulkarim foyzulkarim changed the title Feature/01 setup server Setup server, Enhance Request Tracking, Error Handling, and Logging Apr 7, 2024
@foyzulkarim foyzulkarim marked this pull request as ready for review April 7, 2024 09:02
@foyzulkarim foyzulkarim merged commit 1d19e91 into main Apr 7, 2024
foyzulkarim added a commit that referenced this pull request Apr 7, 2024
Setup server, Enhance Request Tracking, Error Handling, and Logging
@foyzulkarim foyzulkarim deleted the feature/01-setup-server branch April 7, 2024 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant