Merge pull request #28 from sczheng189/main
Best effort sanitize error objects to ensure serialization
This commit is contained in:
commit
ab8566a786
1 changed files with 23 additions and 4 deletions
27
server.py
27
server.py
|
|
@ -111,8 +111,8 @@ OPENAI_MODELS = [
|
||||||
|
|
||||||
# List of Gemini models
|
# List of Gemini models
|
||||||
GEMINI_MODELS = [
|
GEMINI_MODELS = [
|
||||||
"gemini-2.5-pro-preview-03-25",
|
"gemini-2.5-flash",
|
||||||
"gemini-2.0-flash"
|
"gemini-2.5-pro"
|
||||||
]
|
]
|
||||||
|
|
||||||
# Helper function to clean schema for Gemini
|
# Helper function to clean schema for Gemini
|
||||||
|
|
@ -1337,8 +1337,27 @@ async def create_message(
|
||||||
if key not in error_details and key not in ['args', '__traceback__']:
|
if key not in error_details and key not in ['args', '__traceback__']:
|
||||||
error_details[key] = str(value)
|
error_details[key] = str(value)
|
||||||
|
|
||||||
# Log all error details
|
# Helper function to safely serialize objects for JSON
|
||||||
logger.error(f"Error processing request: {json.dumps(error_details, indent=2)}")
|
def sanitize_for_json(obj):
|
||||||
|
"""递归地清理对象使其可以JSON序列化"""
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
return {k: sanitize_for_json(v) for k, v in obj.items()}
|
||||||
|
elif isinstance(obj, list):
|
||||||
|
return [sanitize_for_json(item) for item in obj]
|
||||||
|
elif hasattr(obj, '__dict__'):
|
||||||
|
return sanitize_for_json(obj.__dict__)
|
||||||
|
elif hasattr(obj, 'text'):
|
||||||
|
return str(obj.text)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
json.dumps(obj)
|
||||||
|
return obj
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return str(obj)
|
||||||
|
|
||||||
|
# Log all error details with safe serialization
|
||||||
|
sanitized_details = sanitize_for_json(error_details)
|
||||||
|
logger.error(f"Error processing request: {json.dumps(sanitized_details, indent=2)}")
|
||||||
|
|
||||||
# Format error for response
|
# Format error for response
|
||||||
error_message = f"Error: {str(e)}"
|
error_message = f"Error: {str(e)}"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue