Fix HTTP 500 error in ObjectSerializer by handling non-serializable objects gracefully
Co-authored-by: alexta69 <7450369+alexta69@users.noreply.github.com>
This commit is contained in:
parent
34b6534973
commit
71c1b2f9ce
1 changed files with 8 additions and 4 deletions
12
app/main.py
12
app/main.py
|
|
@ -123,10 +123,14 @@ class ObjectSerializer(json.JSONEncoder):
|
||||||
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes)):
|
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes)):
|
||||||
try:
|
try:
|
||||||
return list(obj)
|
return list(obj)
|
||||||
except:
|
except (TypeError, ValueError, RuntimeError):
|
||||||
pass
|
# If conversion to list fails, log a warning and return a placeholder
|
||||||
# Fall back to default behavior
|
log.warning(f"Failed to convert {type(obj).__name__} to list, using string representation")
|
||||||
return json.JSONEncoder.default(self, obj)
|
return str(obj)
|
||||||
|
# For objects that can't be serialized, return their string representation
|
||||||
|
# This prevents HTTP 500 errors while still providing useful information
|
||||||
|
log.warning(f"Object of type {type(obj).__name__} is not directly JSON serializable, using string representation")
|
||||||
|
return str(obj)
|
||||||
|
|
||||||
serializer = ObjectSerializer()
|
serializer = ObjectSerializer()
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue