-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add global exception handling and commin HTML template #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Add global exception handling and commin HTML template #6
Conversation
Signed-off-by: Mohamed Sharfan <sharfansaleem72@gmail.com>
There was a problem hiding this 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
RequestValidationErrorand 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(), | ||
| }, | ||
| ) |
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
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.
| ) | |
| ) |
| from . import __version__ | ||
| from utils.constants import DEFAULT_GREETING | ||
| from utils.helper import normalize_name | ||
| import streamlit as st |
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
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.
| 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) |
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
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.
| apply_common_styles() | ||
| st.write("Welcome to the app!") | ||
| start() | ||
|
|
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
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.
| apply_common_styles() | |
| st.write("Welcome to the app!") | |
| start() | |
| start() |
| content = { | ||
| "status": "error", | ||
| "message": "An internal server error occured", | ||
| "details": str(exc), |
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
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.
| 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 = { |
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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:
api/main.py): Added global exception handlers forRequestValidationError(422) and generalException(500). The API now returns structured JSON responses instead of crashing or leaking stack traces.app.py): Created anapply_common_styles()function to inject consistent CSS styling and a standardized header across the app.Fixes #5
Type of change
How Has This Been Tested?
I have verified these changes locally using the following methods:
uvicornand sent invalid JSON payloads via Postman/Curl. Verified that the API returns a clean 422 JSON error message instead of a server crash.streamlit run app.pyand confirmed that the custom CSS styles and common header are correctly applied to the interface.Test Configuration:
Checklist