Skip to content

Comments

feat:Global Error Handlers #28#32

Open
abhayrajjais01 wants to merge 4 commits intoOSeMOSYS:masterfrom
abhayrajjais01:fix/issue-28-error-handlers
Open

feat:Global Error Handlers #28#32
abhayrajjais01 wants to merge 4 commits intoOSeMOSYS:masterfrom
abhayrajjais01:fix/issue-28-error-handlers

Conversation

@abhayrajjais01
Copy link

Description

This PR addresses Issue #28 by implementing standardized global JSON error handlers for 404 Not Found and 500 Internal Server Error in the Flask backend.

Note: Since the codebase routes 404s to the frontend React app by default, special care was taken to only return JSON 404s for API-specific routes to avoid breaking the frontend router.

What was changed

  • Added an @app.errorhandler(404) in API/app.py. It checks the request headers (Accept: application/json) and path (/api/) to determine if it should return a standardized JSON error message or fall back to the React index page.
  • Added a global Exception handler @app.errorhandler(Exception) that catches global server-side exceptions (500s) and returns a uniform JSON response. This was necessary to ensure the WSGI web server (waitress) propagates the formatted JSON error to the client instead of standard HTML debugging logs.

Verification

  • Triggered a 404 on an API route (/api/non-existent) and verified the correct JSON error response block was returned.
  • Confirmed that non-API routes still serve the frontend static index.html.
  • Triggered an induced Exception during testing and verified the internal server error is caught and formatted properly as JSON.

Fixes #28

@abhayrajjais01 abhayrajjais01 changed the title feat: Add /health endpoint for backend status (Fixes #29) feat:Global Error Handlers #28 Feb 20, 2026
@arnchlmcodes
Copy link

Hey, I noticed there's a related PR (#33) introducing an api_response() helper to standardize response formatting across routes. Since the error handlers here currently construct the response dictionary manually, I was wondering if it might be worth considering using that helper once it lands. That could help keep success and error responses aligned from a single source of truth and avoid small format differences over time. Happy to hear what you think

@abhayrajjais01
Copy link
Author

"Thanks for the review, @arnchlmcodes , I completely agree that standardizing our responses is the way to go here i will pull in the changes from PR #33 and refactor these error handlers to use the api_response helper to ensure consistency

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.

Add global Flask error handlers for 404 and 500 responses

3 participants