Lite: Error handling after initialization (#6005)

* Update WorkerProxy to emit a unified `error` event for any type error and a `run-start` event when `run_file` or `run_code` is called, and update Index.svelte to handle these events

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Yuichiro Tachibana (Tsuchiya) 2023-10-19 17:39:23 +09:00 committed by GitHub
parent 584fd97d59
commit e0ed0642ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 16 deletions

View File

@ -0,0 +1,7 @@
---
"@gradio/app": minor
"@gradio/wasm": minor
"gradio": minor
---
feat:Lite: Error handling after initialization

View File

@ -98,7 +98,15 @@
worker_proxy.addEventListener("progress-update", (event) => {
loading_text = (event as CustomEvent).detail + "...";
});
worker_proxy.addEventListener("initialization-error", (event) => {
worker_proxy.addEventListener("run-start", (event) => {
status = {
message: "",
load_status: "pending",
status: "sleeping",
detail: "SLEEPING"
}
});
worker_proxy.addEventListener("error", (event) => {
const error: Error = (event as CustomEvent).detail;
// XXX: Although `status` is expected to store Space status info,

View File

@ -57,7 +57,7 @@ export class WorkerProxy extends EventTarget {
error
);
this.dispatchEvent(
new CustomEvent("initialization-error", {
new CustomEvent("error", {
detail: error
})
);
@ -65,23 +65,43 @@ export class WorkerProxy extends EventTarget {
}
public async runPythonCode(code: string): Promise<void> {
await this.postMessageAsync({
type: "run-python-code",
data: {
code
}
});
this.firstRunPromiseDelegate.resolve();
this.dispatchEvent(new Event("run-start"));
try {
await this.postMessageAsync({
type: "run-python-code",
data: {
code
}
});
this.firstRunPromiseDelegate.resolve();
} catch (error) {
this.dispatchEvent(
new CustomEvent("error", {
detail: error
})
);
throw error;
}
}
public async runPythonFile(path: string): Promise<void> {
await this.postMessageAsync({
type: "run-python-file",
data: {
path
}
});
this.firstRunPromiseDelegate.resolve();
this.dispatchEvent(new Event("run-start"));
try {
await this.postMessageAsync({
type: "run-python-file",
data: {
path
}
});
this.firstRunPromiseDelegate.resolve();
} catch (error) {
this.dispatchEvent(
new CustomEvent("error", {
detail: error
})
);
throw error;
}
}
// A wrapper for this.worker.postMessage(). Unlike that function, which
@ -186,6 +206,13 @@ export class WorkerProxy extends EventTarget {
data,
opts
}
}).catch((error) => {
this.dispatchEvent(
new CustomEvent("error", {
detail: error
})
);
throw error;
}) as Promise<void>;
}
@ -196,6 +223,13 @@ export class WorkerProxy extends EventTarget {
oldPath,
newPath
}
}).catch((error) => {
this.dispatchEvent(
new CustomEvent("error", {
detail: error
})
);
throw error;
}) as Promise<void>;
}
@ -205,6 +239,13 @@ export class WorkerProxy extends EventTarget {
data: {
path
}
}).catch((error) => {
this.dispatchEvent(
new CustomEvent("error", {
detail: error
})
);
throw error;
}) as Promise<void>;
}
@ -214,6 +255,13 @@ export class WorkerProxy extends EventTarget {
data: {
requirements
}
}).catch((error) => {
this.dispatchEvent(
new CustomEvent("error", {
detail: error
})
);
throw error;
}) as Promise<void>;
}