feat: Enhance exception handling for openrouterkey endpoint with case-insensitive path checks

This commit is contained in:
Slipstream 2025-05-14 17:38:21 -06:00
parent fede9ae99e
commit f5cfa7a00a
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -402,13 +402,21 @@ app = FastAPI(title="Unified API Service", lifespan=lifespan, debug=True)
@app.exception_handler(StarletteHTTPException) @app.exception_handler(StarletteHTTPException)
async def teapot_override(request: Request, exc: StarletteHTTPException): async def teapot_override(request: Request, exc: StarletteHTTPException):
# Check if this is a request to the openrouterkey endpoint # Check if this is a request to an openrouterkey endpoint (case-insensitive check for 'openrouterkey' part)
if request.url.path == "/openrouterkey" or request.url.path.endswith("/openrouterkey"): path_lower = str(request.url.path).lower() # Ensure path is string and lowercased
# Don't convert 404 errors for the openrouterkey endpoint to 418 is_openrouterkey_related_path = (
log.warning(f"Exception in openrouterkey endpoint: {exc.status_code} - {exc.detail}") path_lower == "/openrouterkey" or
raise exc path_lower == "/api/openrouterkey" or
path_lower == "/discordapi/openrouterkey"
)
# For all other 404 errors, return a teapot response if is_openrouterkey_related_path:
# For openrouterkey related paths, log the actual exception and re-raise it.
# This means if it's a 404, it stays a 404. If it's 403, it stays 403, etc.
log.warning(f"Exception for openrouterkey path '{request.url.path}': {exc.status_code} - {exc.detail}")
raise exc # Re-raise the original exception
# For all other paths that result in a 404:
if exc.status_code == 404: if exc.status_code == 404:
log.info(f"Converting 404 to 418 teapot for path: {request.url.path}") log.info(f"Converting 404 to 418 teapot for path: {request.url.path}")
html_content = """ html_content = """