fix: improve error logging

This commit is contained in:
Alex
2025-02-06 19:44:09 +00:00
parent ad051ed083
commit 0c4c4d5622
2 changed files with 87 additions and 52 deletions

View File

@@ -263,13 +263,12 @@ def complete_stream(
data = json.dumps({"type": "end"})
yield f"data: {data}\n\n"
except Exception as e:
print("\033[91merr", str(e), file=sys.stderr)
traceback.print_exc()
logger.error(f"Error in stream: {str(e)}")
logger.error(traceback.format_exc())
data = json.dumps(
{
"type": "error",
"error": "Please try again later. We apologize for any inconvenience.",
"error_exception": str(e),
}
)
yield f"data: {data}\n\n"
@@ -384,7 +383,7 @@ class Stream(Resource):
except ValueError:
message = "Malformed request body"
print("\033[91merr", str(message), file=sys.stderr)
current_app.logger.error(f"/stream - error: {message}")
return Response(
error_stream_generate(message),
status=400,
@@ -395,13 +394,9 @@ class Stream(Resource):
f"/stream - error: {str(e)} - traceback: {traceback.format_exc()}",
extra={"error": str(e), "traceback": traceback.format_exc()},
)
message = e.args[0]
status_code = 400
# Custom exceptions with two arguments, index 1 as status code
if len(e.args) >= 2:
status_code = e.args[1]
return Response(
error_stream_generate(message),
error_stream_generate('Unknown error occurred'),
status=status_code,
mimetype="text/event-stream",
)