Skip to content

Commit

Permalink
Merge pull request #598 from TeskaLabs/feature/json-ser-dataclasses
Browse files Browse the repository at this point in the history
JSON serializer can handle dataclasses + better exception handling in threadsafe PubSub
  • Loading branch information
ateska authored Aug 20, 2024
2 parents d858694 + 01e07e0 commit 6bfc2d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion asab/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ def publish_threadsafe(self, message_type: str, *args, **kwargs):
asynchronously (bool, optional): If `True`, `call_soon()` method will be used for the asynchronous delivery of the message. Defaults to `False`.
"""
def in_main_thread():
self.publish(message_type, *args, **kwargs)
try:
self.publish(message_type, *args, **kwargs)
except Exception:
L.exception("Error in a PubSub threadsafe", struct_data={'message_type': message_type})
self.Loop.call_soon_threadsafe(in_main_thread)


Expand Down
10 changes: 8 additions & 2 deletions asab/web/rest/json.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import functools
import json
import logging
import uuid
import logging
import datetime
import functools
import dataclasses


import aiohttp.web
import fastjsonschema

Expand Down Expand Up @@ -62,6 +65,9 @@ def default(self, o):
elif isinstance(o, bytes):
return o.hex()

elif dataclasses.is_dataclass(o):
return dataclasses.asdict(o)

try:
return json.JSONEncoder.default(self, o)
except TypeError:
Expand Down

0 comments on commit 6bfc2d8

Please sign in to comment.