Skip to content

Commit 5f49928

Browse files
committed
fix: Check plan outcome when calling Plan objects
When using the client.plans.plan_name() approach to running plans, the task status returned by the server was not being checked so that plans failing did not raise exceptions on the client side. Checking the task status also allows values returned by plans to be accessed easily if they were serializable.
1 parent a7cb17f commit 5f49928

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/blueapi/client/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from functools import cached_property
77
from itertools import chain
88
from pathlib import Path
9-
from typing import Self
9+
from typing import Any, Self
1010

1111
from bluesky_stomp.messaging import MessageContext, StompClient
1212
from bluesky_stomp.models import Broker
@@ -38,7 +38,7 @@
3838
)
3939
from blueapi.utils import deprecated
4040
from blueapi.worker import WorkerEvent, WorkerState
41-
from blueapi.worker.event import ProgressEvent, TaskStatus
41+
from blueapi.worker.event import ProgressEvent, TaskError, TaskResult, TaskStatus
4242
from blueapi.worker.task_worker import TrackableTask
4343

4444
from .event_bus import AnyEvent, EventBusClient, OnAnyEvent
@@ -141,13 +141,17 @@ def __init__(self, name, model: PlanModel, client: "BlueapiClient"):
141141
self._client = client
142142
self.__doc__ = model.description
143143

144-
def __call__(self, *args, **kwargs):
144+
def __call__(self, *args, **kwargs) -> Any:
145145
req = TaskRequest(
146146
name=self.name,
147147
params=self._build_args(*args, **kwargs),
148148
instrument_session=self._client.instrument_session,
149149
)
150-
self._client.run_task(req)
150+
match self._client.run_task(req):
151+
case TaskStatus(result=TaskResult(result=res)):
152+
return res
153+
case TaskStatus(result=TaskError(type=typ, message=msg)):
154+
raise PlanFailedError(typ, msg)
151155

152156
@property
153157
def help_text(self) -> str:
@@ -744,3 +748,9 @@ def login(self, token_path: Path | None = None):
744748
auth.start_device_flow()
745749
else:
746750
print("Server is not configured to use authentication!")
751+
752+
753+
class PlanFailedError(Exception):
754+
def __init__(self, typ: str, message: str):
755+
super().__init__(message)
756+
self._type = typ

0 commit comments

Comments
 (0)