Skip to content

Add Global Error Handling Middleware for Consistent Error Responses - #27

Open
ranjhayhero wants to merge 6 commits into
HermanL0201:e76a0895-0a2b-426e-b43b-059fa02aee8ffrom
ranjhayhero:feat-global-error-middleware-1749263372
Open

Add Global Error Handling Middleware for Consistent Error Responses#27
ranjhayhero wants to merge 6 commits into
HermanL0201:e76a0895-0a2b-426e-b43b-059fa02aee8ffrom
ranjhayhero:feat-global-error-middleware-1749263372

Conversation

@ranjhayhero

@ranjhayhero ranjhayhero commented Jun 7, 2025

Copy link
Copy Markdown

Add Global Error Handling Middleware for Consistent Error Responses

Description

Task

Add basic error handling middleware

Acceptance Criteria

  • Global error middleware captures all unhandled errors
  • Error responses have consistent JSON structure
  • Development environments include error stack trace
  • Production environments hide implementation details
  • Custom errors support specific status codes

Summary of Work

Overview

This pull request implements a comprehensive global error handling middleware to provide consistent error responses across the API.

Key Changes

  • Created errorHandler.ts middleware in src/middleware/
  • Implemented AppError custom error class for more granular error handling
  • Added environment-based error response with optional stack trace in development

Implementation Details

Error Handler Middleware

  • Catches and processes unhandled errors
  • Logs errors on the server-side
  • Determines appropriate HTTP status code
  • Provides a standardized error response format
  • Conditionally includes stack trace in development environment

Custom AppError Class

  • Allows throwing errors with custom status codes
  • Extends built-in Error class
  • Provides more context for error handling

Error Response Structure

{
  success: false,
  status: number,
  message: string,
  stack?: string // Only in development
}

Considerations

  • Default to 500 Internal Server Error
  • Preserve original status code when available
  • Provide meaningful error messages
  • Protect sensitive information in production

Changes Made

  • Created src/middleware/errorHandler.ts
  • Implemented AppError custom error class
  • Added global error handling logic
  • Implemented environment-based error response

Tests

  • Verify error handler returns correct status code
  • Confirm error response structure matches specification
  • Validate stack trace appears only in development
  • Ensure default 500 status code for unhandled errors
  • Test custom AppError with specific status codes

Signatures

Staking Key

HUjFCWMEK2VxfsrCpayemKNcsvvevebHnM8xDRqPH6os: 3puBiJfK9NaQoJWP5agHvQo3GTbrZ5bPQv5EkUesZYJGxYY8JrJqy5Ehwf2rritBJdXPNZUkUR6A17fbrqhsTAb51pMDbeCCBSbTHLnj5QzP88bw3hqNC17U6UNEC4TZyP3N8PYqaw76JcpRBxqADHjcJGWmucZpBmF7PZ9LXVmdM7K22Xg5RfyiyNzYfTp8LdGbHhKzaWf5YwmvgBpCy2SqYsttKjC9TAJ78oXHnpACwGWQtsFgCNnJFriHDm8Z7uo7kCQ26cKa3ESyu1TXdk5M59QhswWxHuk59HhH7qi6gnwoKTUfhe8FZhPSBSmAVVbPeTGUaXTuSpLRFcpnKwJWQ3VmZgfewqLpDsJixMLHCQAxNpNdEcsD5Zd7tzRNBwVYewB4V1RG5BGtUCRN39ozb9CqL8wkjxC4

Public Key

21bw5w1jncJHRHNs2CwqSGC2JRdMVFPHx9F1nyMLQ61C: 2iDgdEedob7G1SkNvzzQdFie4Pj9Fd1EjXoaDeqAhMTHdBZqweJ1EUXtDycBRP8fJ34etg6mB6nduBtHXTEhoTyRSQFgjDPEq3jQJ2c4yYC3tXzH9yyRg26GzABnMto8U1z2fc1Bt5BfSvAjmWbLQSGTw3aiBEQcGB7TQsf3sXhpzBY6t5T4mm8oEJo3ACfUHfRh9JFjhwHYy7yEwrY7KZLfnwbSFR4qgLH6xHq3k7Vtd9jXAUPDodJAE6C782fedSB63RUokqDNEdTfeKC2ihEjCLdz168GsNtvwtrjtk5gcn8SFWwH9yBnppr5ikeJEoFxSTQ864swkAyxNgKFqNc3TPTLHaqvQwx4o7bMq9i9MLjNLzv4Rj7rqaZTkk1rqVSZQNZA2jSMHSqGQVxrQCsKbihYZBq9DoKJ

@ranjhayhero ranjhayhero changed the title [WIP] Implement Global Error Handling Middleware for Hero API Add Global Error Handling Middleware for Consistent Error Responses Jun 7, 2025
@ranjhayhero
ranjhayhero marked this pull request as ready for review June 7, 2025 02:33
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