Skip to content

FEATURE REQUEST: Centralized and Environment-Aware Error Handling #171

@aneesafatima

Description

@aneesafatima

Is your feature request related to a problem?

  • Yes, it is related to a problem

Describe the feature you'd like

🌟 Feature Description

Introduce a centralized and environment-aware error handling mechanism across all backend routes to reduce duplication, improve consistency, and clearly separate development-level and production-level error responses.

🔍 Problem Statement

While going through the backend routes (appointments, blood pressure, discharge, medicine, profile, etc.), I noticed that error handling is currently done inside individual route handlers.

Some common patterns I observed across all routes:

  • Repeated try/except sqlite3.OperationalError blocks
  • Repeated "Database Error" responses
  • Manual validation and error responses inside each route
  • Slightly different error formats and behaviors across routes
  • No clear distinction between development and production error responses

As the number of routes grows, this leads to:

  • Duplicate logic across files
  • Inconsistent API error responses
  • Harder debugging during development
  • Reduced maintainability and scalability

At the moment, routes either expose raw database errors or hide all error details entirely, regardless of environment.

🎯 Expected Outcome

The goal is not to change business logic, but to improve how errors are handled globally.

Expected improvements:

  • Centralized error handling instead of per-route repetition
  • Consistent JSON error response structure across all APIs
  • Clear separation between:
    • Development errors (detailed messages, useful for debugging)
    • Production errors (sanitized, user-safe responses)
  • Cleaner and more readable route handlers
  • Easier onboarding for future contributors

📷 Screenshots and Design Ideas

Conceptual design (high level):

  • Define centralized error handlers using Flask's error handling mechanisms
  • Optionally introduce custom exception classes (e.g. validation, database, not-found)
  • Use an environment variable (e.g. FLASK_ENV, DEBUG, or a custom config) to determine:
    • What level of error detail should be returned
  • Routes raise or propagate errors instead of manually constructing responses everywhere

This approach is conceptually similar to centralized error middleware in Node.js/Express, adapted to Flask's ecosystem.

📋 Additional Context

I want to be transparent here: I have implemented centralized error handling in Node.js (Express) before, where errors passed to middleware are handled in a single place with environment-based behavior.

I have not implemented the same pattern in Flask yet, but based on Flask's error handler system, I believe the same idea can be replicated cleanly here using Flask-native patterns.

I am raising this issue to:

  • Confirm whether this aligns with the project's architecture and goals
  • Get maintainer feedback on whether centralized and environment-aware error handling would be a desired improvement
  • Avoid making architectural changes without discussion

If this direction makes sense, I would be happy to work on a minimal, incremental implementation (for example, introducing the central handler and updating a small subset of routes first).

Record

  • I agree to follow this project's Code of Conduct
  • I want to work on implementing this feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions