Skip to content

Commit c9b0118

Browse files
authored
Merge pull request #58 from DiamondLightSource/17-improve-submit-plan
Add error handling to blueapi UI utilities
2 parents f5c536a + 3ff8a89 commit c9b0118

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

src/blueapi/blueapi.ts

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { useQuery, UseQueryResult } from "react-query";
22

3-
const BLUEAPI_SOCKET = import.meta.env.VITE_BLUEAPI_SOCKET;
3+
const BLUEAPI_SOCKET: string = import.meta.env.VITE_BLUEAPI_SOCKET;
4+
5+
type BlueApiRequestBody = {
6+
planName: string;
7+
planParams: object;
8+
};
9+
410
export type BlueApiWorkerState =
511
| "IDLE"
612
| "RUNNING"
@@ -13,9 +19,14 @@ export type BlueApiWorkerState =
1319
| "PANICKED"
1420
| "UNKNOWN";
1521

16-
function blueApiCall(endpoint: string, method?: string, body?: object) {
22+
function blueApiCall(
23+
endpoint: string,
24+
method?: string,
25+
body?: object
26+
): Promise<Response> {
1727
const _method = method ?? "GET";
18-
return fetch(BLUEAPI_SOCKET + endpoint, {
28+
const fullUrl = BLUEAPI_SOCKET + endpoint;
29+
return fetch(fullUrl, {
1930
headers: {
2031
Accept: "application/json",
2132
"Content-Type": "application/json",
@@ -60,27 +71,41 @@ export function getWorkerStatus(): Promise<BlueApiWorkerState> {
6071
return blueApiCall("/worker/state").then((res) => res.json());
6172
}
6273

63-
export function submitPlan(
64-
planName: string,
65-
planParams: object
66-
): Promise<string> {
74+
// Note. fetch only rejects a promise on network errors, but http errors
75+
// must be caught by checking the response
76+
function submitTask(request: BlueApiRequestBody): Promise<string | void> {
6777
return blueApiCall("/tasks", "POST", {
68-
name: planName,
69-
params: planParams,
70-
}).then((res) => res.json().then((res) => res["task_id"]));
78+
name: request.planName,
79+
params: request.planParams,
80+
}).then((res) => {
81+
if (!res.ok) {
82+
throw new Error(
83+
`Unable to POST request, response error ${res.statusText}`
84+
);
85+
}
86+
res.json().then((res) => res["task_id"]);
87+
});
88+
}
89+
90+
function runTask(taskId: string): Promise<string | void> {
91+
return blueApiCall("/worker/task", "PUT", { task_id: taskId }).then((res) => {
92+
if (!res.ok) {
93+
throw new Error(`Unable to run task, response error ${res.statusText}`);
94+
}
95+
res.json().then((res) => res["task_id"]);
96+
});
7197
}
7298

7399
export function submitAndRunPlanImmediately(
74-
planName: string,
75-
planParams: object
76-
): Promise<string> {
77-
return submitPlan(planName, planParams).then((res) =>
78-
// TODO make sure submitPlan was succesful before then putting it to the worker
79-
// See https://github.com/DiamondLightSource/mx-daq-ui/issues/17
80-
blueApiCall("/worker/task", "PUT", { task_id: res }).then((res) =>
81-
res.json().then((res) => res["task_id"])
82-
)
83-
);
100+
request: BlueApiRequestBody
101+
): Promise<string | void> {
102+
return submitTask(request)
103+
.then((res) => {
104+
if (res) {
105+
runTask(res);
106+
}
107+
})
108+
.catch((error) => console.log(error));
84109
}
85110

86111
export function abortCurrentPlan(): Promise<BlueApiWorkerState> {

0 commit comments

Comments
 (0)