Skip to content

Commit

Permalink
[llm-starter] Handle errors in runJavascript.
Browse files Browse the repository at this point in the history
Fixes #186.
  • Loading branch information
dglazkov committed Nov 9, 2023
1 parent 043c651 commit 56ddcf5
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions seeds/llm-starter/src/nodes/run-javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,29 @@ const runInBrowser = async (
args: string
): Promise<string> => {
const runner = (code: string, functionName: string) => {
return `${code}\nself.onmessage = () => self.postMessage(JSON.stringify(${functionName}(${args})))`;
return `${code}\nself.onmessage = () => self.postMessage({ result: JSON.stringify(${functionName}(${args})) });self.onerror = (e) => self.postMessage({ error: e.message })`;
};

const blob = new Blob([runner(code, functionName)], {
type: "text/javascript",
});

type WebWorkerResultType = "error" | "result";
type WebWorkerResult = {
[x in WebWorkerResultType]: string;
};

const worker = new Worker(URL.createObjectURL(blob));
const result = new Promise<string>((resolve) => {
worker.onmessage = (e) => resolve(e.data);
worker.onmessage = (e) => {
const data = e.data as WebWorkerResult;
if (data.result) {
resolve(data.result);
return;
} else if (data.error) {
throw new Error(data.error);
}
};
});
worker.postMessage("please");
return result;
Expand Down Expand Up @@ -91,7 +104,7 @@ export const runJavascriptHandler: NodeHandlerFunction = async (
);
return raw ? result : { result };
} catch (e) {
// Remove everthing outside eval from the stack trace
// Remove everything outside eval from the stack trace
const stack = (e as Error).stack;
if (stack !== undefined) {
(e as Error).stack = stack
Expand Down

0 comments on commit 56ddcf5

Please sign in to comment.