Skip to content

Implement Comprehensive Central Error Handling Middleware for Hero API#2

Open
HermanL0201 wants to merge 6 commits into
SammyBryant11:fbcc6015-7663-40e5-9605-a6f7fbc8bbf5from
HermanL0201:feature-global-error-handler-1749252365
Open

Implement Comprehensive Central Error Handling Middleware for Hero API#2
HermanL0201 wants to merge 6 commits into
SammyBryant11:fbcc6015-7663-40e5-9605-a6f7fbc8bbf5from
HermanL0201:feature-global-error-handler-1749252365

Conversation

@HermanL0201
Copy link
Copy Markdown

@HermanL0201 HermanL0201 commented Jun 6, 2025

Implement Comprehensive Central Error Handling Middleware for Hero API

Description

Task

Create a central error handling middleware for the Hero API

Acceptance Criteria

  • Middleware can handle different types of errors
  • Provides consistent error response format
  • Logs errors with relevant details
  • Distinguishes between operational and system errors
  • Works with async route handlers
  • Provides environment-specific error details

Summary of Work

Overview

This pull request implements a robust, centralized error handling middleware for the Hero API, providing consistent error management, logging, and response handling across the application.

Key Features

  • Custom AppError class for detailed error handling
  • Centralized error logging with Winston
  • Environment-aware error response
  • Async error handling wrapper
  • Comprehensive error information capture

Implementation Details

1. Custom Error Class: AppError

  • Extends standard Error class
  • Adds statusCode and isOperational properties
  • Maintains correct prototype chain
  • Allows creating structured, predictable errors

2. Logging Mechanism

  • Uses Winston for structured logging
  • Logs to both console and file
  • Captures error details including:
    • Timestamp
    • Error message
    • Stack trace
    • Request method
    • Request path
    • Operational status

3. Error Handler Middleware

  • Handles both operational and unexpected errors
  • Determines appropriate HTTP status code
  • Provides environment-specific error responses
    • Production: Generic error message
    • Development: Detailed error stack trace
  • Consistent JSON error response format

4. Async Error Handling

  • asyncHandler utility for wrapping async route handlers
  • Automatically catches and forwards async errors
  • Simplifies error management in route implementations

Testing Approach

  • Comprehensive unit tests in tests/middleware/errorHandler.test.ts
  • Covers various error scenarios
  • Validates logging and response mechanisms

Notes

  • Ensure proper error instance type checking
  • Use next(error) in route handlers for automatic error capture
  • Prefer throwing AppError for expected error scenarios

Changes Made

  • Created centralized error handling middleware
  • Implemented AppError custom error class
  • Added Winston logging configuration
  • Created async error handling wrapper
  • Implemented environment-aware error responses

Tests

  • Verify AppError class creates correct error instances
  • Test error handler middleware response formats
  • Validate logging for different error types
  • Check async error handler wrapping functionality
  • Ensure development vs production error responses differ

Signatures

Staking Key

DZwmqbbKALnQYMhTLYdYeBBKVNptDMSAKQjaASJ9btgo: 3MtkH7ztt5jdUPN6fnyohdFVz9ANMh731nHfztzMmH1mbmirQ1qXEGeycZJNtXNRgeMtCb65nY5dXcASZdQnJiCcFQEjS6zegh28Hj7FHn3XfKYRcpN3gB4LypxAhDbQYHBvW5LUaPsLyoJdoNHpJxK2Whmkoy8S1LEnWCEFAep9d3BSxVDztmRGZSVdq5imMT3cpDaZnLNZhSswTvwr8xxiH7HXMapfDdS1XasBGw1nSXyx5kQpHtFd2R5n94bwonVuswAa4LUbbfMoroENYJpLkUYxqdvh73Z2GP46j8aM9aWDxToWAGmQ4QGnkgrjjuWuVRA6T5wqdBcjQMuQkCppTy9DpoEwcptEZHRTG68u7Rgq7ygnUimGByq8csqBtz3BZcw28VSny1nseJrRaJ9yKaTi6xx1gCFE

Public Key

4E3nQUGBiknkTFLJE6Htj3KKNnHzpxhNv2Jw5cvW4s7J: VXSripkUGdE74UtQk63b9ikRZwc7xzhWu8MRLLCboKpbTtytxU6kMvKZh93u2FwRYzjmCTE1833MJaMuTunk9gMsX5tZFV9z6LZgLNLKHxS3xYoP5nfopAJgimdE8NmiHYAH4mXyTp5yWseuR5yb2jqAXqYK87nUFTrUBLCPvqBh7EBpAhdqcx3sMDfa9d5hVqUt16WM2vf2g1wknShKFT1EV1f1YGriQTqjLr9to3DxjTi6bXcnwDmoqLBfkb8nqQxVfVZDFagqxXTKgAeXWHXQNo8Xc64gYfFSGtrtVjunT78GsSxJ3L3K5S5cXvBeXoQYkjJxVFD7NoNGWSgRWYEXJs15sAP4xhur3M1wewX7Zrtx9XMus6DvD883xRsSZyoB7DySsaC6hx8dYBV2eL5je8qQtcimVzL

@HermanL0201 HermanL0201 changed the title [WIP] Implement Centralized Error Handling Middleware for Hero API Implement Comprehensive Central Error Handling Middleware for Hero API Jun 6, 2025
@HermanL0201 HermanL0201 marked this pull request as ready for review June 6, 2025 23:28
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.

1 participant