Skip to content

Commit 1512aaf

Browse files
committed
Log exceptions from commands
1 parent c86f760 commit 1512aaf

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/fastcs/methods/command.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
from types import MethodType
33

44
from fastcs.controllers import BaseController
5+
from fastcs.logging import bind_logger
56
from fastcs.methods.method import Controller_T, Method
67

8+
logger = bind_logger(logger_name=__name__)
9+
710
UnboundCommandCallback = Callable[[Controller_T], Coroutine[None, None, None]]
811
"""A Command callback that is unbound and must be called with a `Controller` instance"""
912
CommandCallback = Callable[[], Coroutine[None, None, None]]
@@ -30,6 +33,17 @@ def _validate(self, fn: CommandCallback) -> None:
3033
async def __call__(self):
3134
return await self._fn()
3235

36+
@property
37+
def fn(self):
38+
async def command():
39+
try:
40+
return await self._fn()
41+
except Exception:
42+
logger.exception("Command failed", fn=self._fn)
43+
raise
44+
45+
return command
46+
3347

3448
class UnboundCommand(Method[Controller_T]):
3549
"""A wrapper of an unbound `Controller` method to be bound into a `Command`.

0 commit comments

Comments
 (0)