Skip to content

[GSSoC26] feat: Add consistent API error response format middleware#457

Open
ashrion wants to merge 1 commit into
Nsanjayboruds:mainfrom
ashrion:feat/consistent-error-format
Open

[GSSoC26] feat: Add consistent API error response format middleware#457
ashrion wants to merge 1 commit into
Nsanjayboruds:mainfrom
ashrion:feat/consistent-error-format

Conversation

@ashrion

@ashrion ashrion commented Jul 11, 2026

Copy link
Copy Markdown

Description

This PR adds a consistent API error response format across all endpoints.

Changes

  • Created backend/utils/apiError.js with ApiError class and factory methods
  • Updated backend/middleware/errorHandler.js to use consistent error format
  • Standardized error responses: { success, message, errors, timestamp }
  • Added asyncHandler wrapper for automatic async error catching
  • Include stack traces in development mode only
  • Handle common error types: ValidationError, CastError, duplicate keys, JWT errors

Motivation

  • Error responses were inconsistent across different endpoints
  • Some returned { success: false, message, errors }, others { message }, others { error }
  • Consistent format improves frontend error handling and debugging

Testing

  • Trigger validation error and verify response format
  • Trigger 404 and verify format
  • Trigger JWT error and verify format
  • Verify stack trace only appears in development

Closes #456

GSSoC26 contribution

- Create backend/utils/apiError.js with ApiError class and helper methods
- Update backend/middleware/errorHandler.js to use consistent error format
- Standardize error responses with success, message, errors, timestamp
- Add asyncHandler wrapper for automatic error catching
- Include stack traces in development mode only

GSSoC26
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.

[GSSoC26] feat: Add consistent API error response format middleware

1 participant