Skip to content

Refactor: Implement global error handling and clean up controllers (Fixes #447)#450

Open
Jivan-Patel wants to merge 2 commits into
Canopus-Labs:mainfrom
Jivan-Patel:fix/issue-447-global-error-handling
Open

Refactor: Implement global error handling and clean up controllers (Fixes #447)#450
Jivan-Patel wants to merge 2 commits into
Canopus-Labs:mainfrom
Jivan-Patel:fix/issue-447-global-error-handling

Conversation

@Jivan-Patel

Copy link
Copy Markdown
Contributor

🐛 Description

This PR addresses issue #447 by introducing a global error-handling middleware to the Express application and refactoring all backend controllers to adhere to the DRY (Don't Repeat Yourself) principle.

Previously, every controller manually wrapped its logic in a try/catch block and returned a hardcoded 500 response. This resulted in duplicated code (35+ occurrences) and inconsistent error responses across the application.

✅ Changes Made

  • Created middlewares/errorHandler.js: A global Express error-handling middleware that catches errors passed via next(err) and sends a unified, consistent JSON response. It also securely handles stack traces by omitting them in production.
  • Updated server.js: Registered the errorHandler middleware securely at the bottom of the middleware stack (right after the API routes).
  • Refactored 8 Controllers: Replaced all redundant res.status(500) manual catch blocks with next(error) in the following controllers:
    • authController.js
    • aiController.js
    • questionController.js
    • resumeController.js
    • sessionController.js
    • userSheetProgressController.js
    • achievementController.js
    • jobController.js

🎯 Benefits

  • Eliminates 35+ duplicated catch blocks across the codebase, resulting in cleaner controllers focused purely on business logic.
  • Guarantees consistent error responses for all API endpoints.
  • Establishes a single centralized place to add logging, monitoring (e.g., Sentry), or alerting in the future.

Fixes #447

Comment thread backend/middlewares/errorHandler.js Fixed
@KaranUnique

Copy link
Copy Markdown
Contributor

@Jivan-Patel Please take a look on security issue by github ai

@Jivan-Patel

Copy link
Copy Markdown
Contributor Author

@KaranUnique, The vulnerability flagged by the GitHub Advanced Security CodeQL has been resolved. So please review and merged my PR.

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.

refactor(backend): implement global error handling middleware to remove duplicated try/catch blocks

3 participants