From b88c0c0a3f05133f02da9c1c01dad02049a67b74 Mon Sep 17 00:00:00 2001 From: aydnOktay Date: Fri, 20 Feb 2026 01:29:32 +0300 Subject: [PATCH] Fix exception handler bug and improve error logging - Fix critical bug in exception handler where request.body() could fail if body was already consumed - Add safe error handling with try-except for request body reading - Limit request body logging to 1KB to prevent logging large payloads and sensitive data - Add request method to error logs for better debugging - Fix grammar error in README: 'a open-source' -> 'an open-source' --- README.md | 2 +- server/fastapi_server.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a160704..e374bb2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ # 🚀 BitQuant by OpenGradient -**BitQuant** is a open-source AI agent framework for building quantitative AI agents. It leverages specialized models for ML-powered analytics, trading, portfolio management, and more—all through a natural language interface. BitQuant exposes a REST API that turns user inputs like "What is the current risk profile on Bitcoin?" or "Optimize my portfolio for maximum risk-adjusted returns" into actionable insights. +**BitQuant** is an open-source AI agent framework for building quantitative AI agents. It leverages specialized models for ML-powered analytics, trading, portfolio management, and more—all through a natural language interface. BitQuant exposes a REST API that turns user inputs like "What is the current risk profile on Bitcoin?" or "Optimize my portfolio for maximum risk-adjusted returns" into actionable insights. --- diff --git a/server/fastapi_server.py b/server/fastapi_server.py index 6d01da8..9ec33e2 100644 --- a/server/fastapi_server.py +++ b/server/fastapi_server.py @@ -171,7 +171,23 @@ async def generic_exception_handler(request: Request, exc: Exception): logging.error(f"500 Error: {str(exc)}") logging.error(f"Traceback: {error_traceback}") logging.error(f"Request Path: {request.url.path}") - logging.error(f"Request Body: {await request.body()}") + logging.error(f"Request Method: {request.method}") + + # Safely attempt to read request body for debugging + # Note: Request body may already be consumed, so we wrap in try-except + # Also limit body size to prevent logging large payloads + try: + body = await request.body() + # Only log body in development or if it's small (< 1KB) + # This prevents logging sensitive data and large payloads + max_body_log_size = 1024 + if len(body) <= max_body_log_size: + logging.error(f"Request Body: {body.decode('utf-8', errors='ignore')}") + else: + logging.error(f"Request Body: [Too large to log ({len(body)} bytes)]") + except Exception as body_error: + # Request body may have already been consumed + logging.error(f"Could not read request body: {str(body_error)}") return JSONResponse( status_code=500,