Skip to content

Commit 83d1610

Browse files
authored
Merge pull request #250 from DiamondLightSource/tidying
Log shutdown exception for better output Try to give clearer error for p4p Value creation failure Log exceptions in all attribute callbacks
2 parents 69e72d2 + f6dce44 commit 83d1610

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
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:

src/fastcs/control_system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ async def block_forever():
139139
await asyncio.gather(*coros)
140140
except asyncio.CancelledError:
141141
pass
142-
except Exception as e:
143-
raise RuntimeError("Unhandled exception in serve") from e
142+
except Exception:
143+
logger.exception("Unhandled exception in serve")
144144
finally:
145145
logger.info("Shutting down FastCS")
146146
self._stop_scan_tasks()

src/fastcs/transport/epics/pva/pvi_tree.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,18 @@ def make_p4p_value(self) -> Value:
139139
raw_value = self._make_p4p_raw_value()
140140
p4p_type = self._make_type_for_raw_value(raw_value)
141141

142-
return Value(
143-
p4p_type,
144-
{
145-
**p4p_alarm_states(),
146-
**p4p_timestamp_now(),
147-
**display,
148-
"value": raw_value,
149-
},
150-
)
142+
try:
143+
return Value(
144+
p4p_type,
145+
{
146+
**p4p_alarm_states(),
147+
**p4p_timestamp_now(),
148+
**display,
149+
"value": raw_value,
150+
},
151+
)
152+
except KeyError as e:
153+
raise ValueError(f"Failed to create p4p Value from {raw_value}") from e
151154

152155
def make_provider(
153156
self,

0 commit comments

Comments
 (0)