From 427ec85900e45ef96225fd18a685aa0a996e0002 Mon Sep 17 00:00:00 2001 From: Mohamed Sharfan Date: Sun, 18 Jan 2026 13:49:10 +0530 Subject: [PATCH 1/2] feat: Add global exception handling and commin HTML template Signed-off-by: Mohamed Sharfan --- src/api/fast_api.py | 46 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/api/fast_api.py b/src/api/fast_api.py index 0d56f34..da338f7 100644 --- a/src/api/fast_api.py +++ b/src/api/fast_api.py @@ -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(""" + + """, unsafe_allow_html=True) + + st.markdown('
Sample Python App
', unsafe_allow_html=True) + +@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(), + }, + ) +@app.exception_handler(Exception) +async def general_exception_handler(request: Request, exc: Exception): + return JSONResponse( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content = { + "status": "error", + "message": "An internal server error occured", + "details": str(exc), + }, + ) class GreetRequest(BaseModel): name: str @@ -98,4 +139,7 @@ def start(): if __name__ == "__main__": + apply_common_styles() + st.write("Welcome to the app!") start() + From 2c3a0308597fd0caa81502f27aa83d6275844979 Mon Sep 17 00:00:00 2001 From: Sharfan Saleem Date: Sun, 18 Jan 2026 14:42:36 +0530 Subject: [PATCH 2/2] Update src/api/fast_api.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/api/fast_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/fast_api.py b/src/api/fast_api.py index da338f7..550db05 100644 --- a/src/api/fast_api.py +++ b/src/api/fast_api.py @@ -56,7 +56,7 @@ async def general_exception_handler(request: Request, exc: Exception): status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content = { "status": "error", - "message": "An internal server error occured", + "message": "An internal server error occurred", "details": str(exc), }, )