clan-app: Catch method not found error in api

This commit is contained in:
Qubasa
2025-06-04 18:16:58 +02:00
parent 0d79f8de2b
commit 7df19df224
2 changed files with 27 additions and 2 deletions

View File

@@ -70,6 +70,10 @@ def app_run(app_opts: ClanAppOptions) -> int:
status="success", status="success",
) )
# TODO: We have to manually import python files to make the API.register be triggered.
# We NEED to fix this, as this is super unintuitive and error-prone.
import clan_lib.machines.actions # noqa: F401
API.overwrite_fn(list_tasks) API.overwrite_fn(list_tasks)
API.overwrite_fn(open_file) API.overwrite_fn(open_file)
API.overwrite_fn(cancel_task) API.overwrite_fn(cancel_task)

View File

@@ -1,5 +1,5 @@
import schema from "@/api/API.json" with { type: "json" }; import schema from "@/api/API.json" with { type: "json" };
import { API, Error } from "@/api/API"; import { API, Error as ApiError } from "@/api/API";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { Schema as Inventory } from "@/api/Inventory"; import { Schema as Inventory } from "@/api/Inventory";
import { toast, Toast } from "solid-toast"; import { toast, Toast } from "solid-toast";
@@ -17,7 +17,7 @@ export type ApiEnvelope<T> =
data: T; data: T;
op_key: string; op_key: string;
} }
| Error; | ApiError;
export type Services = NonNullable<Inventory["services"]>; export type Services = NonNullable<Inventory["services"]>;
export type ServiceNames = keyof Services; export type ServiceNames = keyof Services;
@@ -48,6 +48,25 @@ const _callApi = <K extends OperationNames>(
method: K, method: K,
args: OperationArgs<K>, args: OperationArgs<K>,
): { promise: Promise<OperationResponse<K>>; op_key: string } => { ): { promise: Promise<OperationResponse<K>>; op_key: string } => {
// if window[method] does not exist, throw an error
if (!(method in window)) {
console.error(`Method ${method} not found on window object`);
// return a rejected promise
return {
promise: Promise.resolve({
status: "error",
errors: [
{
message: `Method ${method} not found on window object`,
code: "method_not_found",
},
],
op_key: "noop",
}),
op_key: "noop",
};
}
const promise = ( const promise = (
window as unknown as Record< window as unknown as Record<
OperationNames, OperationNames,
@@ -56,6 +75,7 @@ const _callApi = <K extends OperationNames>(
) => Promise<OperationResponse<OperationNames>> ) => Promise<OperationResponse<OperationNames>>
> >
)[method](args) as Promise<OperationResponse<K>>; )[method](args) as Promise<OperationResponse<K>>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const op_key = (promise as any)._webviewMessageId as string; const op_key = (promise as any)._webviewMessageId as string;
@@ -94,6 +114,7 @@ export const callApi = async <K extends OperationNames>(
args: OperationArgs<K>, args: OperationArgs<K>,
): Promise<OperationResponse<K>> => { ): Promise<OperationResponse<K>> => {
console.log("Calling API", method, args); console.log("Calling API", method, args);
const { promise, op_key } = _callApi(method, args); const { promise, op_key } = _callApi(method, args);
const toastId = toast.custom( const toastId = toast.custom(