-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,12 @@ | ||||||||||||
| from fastapi import FastAPI, HTTPException, Request | ||||||||||||
| from fastapi import FastAPI, HTTPException, Request, status | ||||||||||||
| from fastapi.responses import HTMLResponse | ||||||||||||
| from fastapi.exceptions import RequestValidationError | ||||||||||||
| from fastapi.responses import JSONResponse | ||||||||||||
| from pydantic import BaseModel | ||||||||||||
| from . import __version__ | ||||||||||||
| from utils.constants import DEFAULT_GREETING | ||||||||||||
| from utils.helper import normalize_name | ||||||||||||
| import streamlit as st | ||||||||||||
|
|
||||||||||||
| app = FastAPI( | ||||||||||||
| title="sample API", | ||||||||||||
|
|
@@ -19,6 +22,44 @@ | |||||||||||
| }, | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| 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) | ||||||||||||
|
Comment on lines
+25
to
+41
|
||||||||||||
|
|
||||||||||||
| @app.exception_handler(RequestValidationError) | ||||||||||||
| async def validation_exception_handler(request: Request, exc: RequestValidationError): | ||||||||||||
| return JSONResponse( | ||||||||||||
| status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, | ||||||||||||
| content = { | ||||||||||||
| "status": "error", | ||||||||||||
| "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.
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
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.
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() |
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.