Skip to content

Commit f6dce44

Browse files
committed
Log exceptions in all attribute callbacks
Update still re-raises to stop the update loop flooding logs
1 parent 60f9c5a commit f6dce44

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/fastcs/attributes.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,14 @@ async def update(self, value: T) -> None:
163163
self._value = self._datatype.validate(value)
164164

165165
if self._on_update_callbacks is not None:
166-
await asyncio.gather(*[cb(self._value) for cb in self._on_update_callbacks])
166+
try:
167+
await asyncio.gather(
168+
*[cb(self._value) for cb in self._on_update_callbacks]
169+
)
170+
except Exception as e:
171+
logger.opt(exception=e).error(
172+
"On update callback failed", attribute=self, value=value
173+
)
167174

168175
def add_on_update_callback(self, callback: AttrOnUpdateCallback[T]) -> None:
169176
"""Add a callback to be called when the value of the attribute is updated
@@ -197,8 +204,8 @@ async def update_attribute():
197204
try:
198205
self.log_event("Update attribute", topic=self)
199206
await update_callback(self)
200-
except Exception:
201-
logger.opt(exception=True).error("Update loop failed", attribute=self)
207+
except Exception as e:
208+
logger.opt(exception=e).error("Update loop failed", attribute=self)
202209
raise
203210

204211
return update_attribute
@@ -246,10 +253,20 @@ async def put(self, setpoint: T, sync_setpoint: bool = False) -> None:
246253
"""
247254
setpoint = self._datatype.validate(setpoint)
248255
if self._on_put_callback is not None:
249-
await self._on_put_callback(self, setpoint)
256+
try:
257+
await self._on_put_callback(self, setpoint)
258+
except Exception as e:
259+
logger.opt(exception=e).error(
260+
"Put failed", attribute=self, setpoint=setpoint
261+
)
250262

251263
if sync_setpoint:
252-
await self._call_sync_setpoint_callbacks(setpoint)
264+
try:
265+
await self._call_sync_setpoint_callbacks(setpoint)
266+
except Exception as e:
267+
logger.opt(exception=e).error(
268+
"Sync setpoint failed", attribute=self, setpoint=setpoint
269+
)
253270

254271
async def _call_sync_setpoint_callbacks(self, setpoint: T) -> None:
255272
if self._sync_setpoint_callbacks:

0 commit comments

Comments
 (0)