Skip to content

[ENHANCEMENT] Add Request ID Middleware and Structured Error Handling#411

Open
Lochit-Vinay wants to merge 6 commits intofireform-core:mainfrom
Lochit-Vinay:add-request-id-error-handling
Open

[ENHANCEMENT] Add Request ID Middleware and Structured Error Handling#411
Lochit-Vinay wants to merge 6 commits intofireform-core:mainfrom
Lochit-Vinay:add-request-id-error-handling

Conversation

@Lochit-Vinay
Copy link
Copy Markdown

[Enhancement] Add request ID middleware and improve error responses

Closes #409

What I did

  • Added a middleware to attach a unique request ID (X-Request-ID) to every request/response

  • Implemented global exception handlers for:

    • HTTP errors
    • validation errors (422)
    • unexpected server errors
  • Standardized error response format across the API


Why this change

While testing the API, I noticed:

  • error responses were inconsistent
  • validation errors from FastAPI were a bit hard to read
  • there was no easy way to trace requests during debugging

This change makes debugging easier and responses more consistent for API consumers.


Example

Request (invalid input)

{
  "template_id": null,
  "input_text": ""
}

Response

{
  "error": {
    "type": "ValidationError",
    "message": "Invalid request data",
    "details": [
      {
        "field": "template_id",
        "issue": "Input should be a valid integer"
      },
      {
        "field": "input_text",
        "issue": "Input text cannot be empty"
      }
    ]
  }
}

How I tested

  • Ran the server locally using uvicorn api.main:app --reload
  • Verified endpoints via /docs
  • Sent invalid requests and confirmed structured 422 responses
  • Checked that X-Request-ID header is present in responses

Files changed

  • api/main.py

Notes

  • No breaking changes
  • Existing routes continue to work as before

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.

[ENHANCEMENT] Add Field-Level Validation Feedback (Human-Readable Errors for Extracted Data)

1 participant