From 041c7921d6d5fb9a6c94a5bacdb01b1fbc1c1f4b Mon Sep 17 00:00:00 2001 From: Tim Walter Date: Tue, 9 Apr 2024 22:45:37 +0200 Subject: [PATCH] Error handling around return statement --- API/function_app.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/API/function_app.py b/API/function_app.py index 25b98e8..11d7a94 100644 --- a/API/function_app.py +++ b/API/function_app.py @@ -14,7 +14,7 @@ import logging from datetime import datetime from azure_openai import InteractWithOpenAI - +import aiohttp # Initialisierung der Funktion App @@ -88,9 +88,14 @@ async def chat(req: func.HttpRequest) -> func.HttpResponse: logging.info(f"Chat-Endpoint came back: Response: {http_status}: {response}") logging.info("InteractWithOpenAI() Context left, About to Return data to Caller!" ) + # Ensure the response is a string encoded in UTF-8 + response_body = response.encode('utf-8').decode('utf-8') - return func.HttpResponse(response, status_code=http_status, headers={"Content-Type": "text/plain"}) - + try: + return func.HttpResponse(response_body, status_code=http_status, headers={"Content-Type": "text/plain; charset=utf-8"}) + except Exception as e: + logging.error(f"Error returning the response: {e}") + return func.HttpResponse("Internal server error", status_code=500) # Ping- und Status-Endpunkte, noch zu implementieren # Ggf. separate Parameter-Klassen für Ping und Status implementieren