Skip to content

Conversation

@MohamedSharfan
Copy link

This PR implements global exception handling for the FastAPI backend and establishes a common HTML/CSS template for the Streamlit frontend. It also addresses documentation cleanup as requested in the issue.

Key Changes:

  • FastAPI (api/main.py): Added global exception handlers for RequestValidationError (422) and general Exception (500). The API now returns structured JSON responses instead of crashing or leaking stack traces.
  • Streamlit (app.py): Created an apply_common_styles() function to inject consistent CSS styling and a standardized header across the app.
  • Cleanup: Removed obsolete documentation files and updated the README with current application details.

Fixes #5

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

I have verified these changes locally using the following methods:

  • FastAPI Exception Test: Ran the API using uvicorn and sent invalid JSON payloads via Postman/Curl. Verified that the API returns a clean 422 JSON error message instead of a server crash.
  • Streamlit UI Test: Ran streamlit run app.py and confirmed that the custom CSS styles and common header are correctly applied to the interface.

Test Configuration:

  • Browser: Edge
  • Device: Local Development Machine (Windows)
  • Toolchain: Poetry / Python 3.x

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Signed-off-by: Mohamed Sharfan <sharfansaleem72@gmail.com>
Copilot AI review requested due to automatic review settings January 18, 2026 08:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to add global exception handling to the FastAPI backend and establish common HTML/CSS templates for Streamlit. However, the implementation places Streamlit-specific code incorrectly within the FastAPI module, which creates architectural issues.

Changes:

  • Added global exception handlers for RequestValidationError and general exceptions in FastAPI
  • Added Streamlit styling function apply_common_styles() (incorrectly placed in FastAPI module)
  • Added Streamlit imports and calls to the FastAPI file's __main__ block

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"message": "Invalid data provided",
"details": exc.errors(),
},
)
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing blank line between the two exception handler decorators. According to PEP 8, there should be two blank lines between top-level function definitions for better readability.

Suggested change
)
)

Copilot uses AI. Check for mistakes.
from . import __version__
from utils.constants import DEFAULT_GREETING
from utils.helper import normalize_name
import streamlit as st
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The streamlit import should not be in the FastAPI module. This creates an unnecessary dependency and couples the API backend with the frontend framework. Streamlit is a UI framework that should only be imported in frontend files like streamlit_app.py.

Copilot uses AI. Check for mistakes.
Comment on lines +25 to +41
def apply_common_styles():
st.markdown("""
<style>
.reportview-container {
background: #f0f2f6;
}
footer {visibility: hidden;}
.main-header {
font-size: 2.5rem;
color: #4B4B4B;
text-align: center;
margin-bottom: 2rem;
}
</style>
""", unsafe_allow_html=True)

st.markdown('<div class="main-header">Sample Python App</div>', unsafe_allow_html=True)
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The apply_common_styles function is Streamlit-specific and should not be in the FastAPI module (fast_api.py). This function should be placed in the Streamlit app file (src/sample/streamlit_app.py) instead. The FastAPI module should only contain API-related code.

Copilot uses AI. Check for mistakes.
Comment on lines +142 to +145
apply_common_styles()
st.write("Welcome to the app!")
start()

Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These Streamlit function calls (apply_common_styles and st.write) should not be in the FastAPI module's main block. The FastAPI module is meant to run the API server, not the Streamlit UI. When running the API with uvicorn or through the CLI command, these calls would fail since Streamlit is not initialized in this context.

Suggested change
apply_common_styles()
st.write("Welcome to the app!")
start()
start()

Copilot uses AI. Check for mistakes.
content = {
"status": "error",
"message": "An internal server error occured",
"details": str(exc),
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing internal exception details (str(exc)) in the API response is a security risk. This can leak sensitive information about the application's internal workings, file paths, or database schema to potential attackers. Consider logging the full exception internally and returning only a generic error message to the client.

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +57
content = {
"status": "error",
"message": "Invalid data provided",
"details": exc.errors(),
},
)
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content = {
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing around the equals sign is inconsistent. There should be no space before the equals sign. This appears on both lines 47 and 57.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

❇️: [RZP-260005]: Update poetry streamlit with common template and exception handling in fastAPI

1 participant