diff --git a/src/js/websocket.ts b/src/js/websocket.ts index 8c31b3a..0fe5230 100644 --- a/src/js/websocket.ts +++ b/src/js/websocket.ts @@ -57,11 +57,11 @@ function retryConnection() { connected = false if (retries >= MAX_RETRIES) { - addErrorMessage(`!! Failed to reconnect ${retries} times. Server is down.`) + addErrorMessage(`Failed to reconnect ${retries} times. Server is down.`) if (ws) ws.onclose = () => { } return } retries++ - addErrorMessage(`!! Connection lost. Retrying...`) + addErrorMessage(`Connection lost. Retrying...`) setTimeout(startConnection, 2000) } diff --git a/src/server.tsx b/src/server.tsx index 5783a4f..b8a9f74 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -12,7 +12,7 @@ import { transpile, isFile, tilde } from "./utils" import { serveApp } from "./webapp" import { initDNS } from "./dns" import { commands, commandPath, loadCommandModule } from "./commands" -import { runInSession, processExecOutput } from "./shell" +import { runCommandFn } from "./shell" import { send, addWebsocket, removeWebsocket, closeWebsockets } from "./websocket" import { Layout } from "./html/layout" @@ -98,12 +98,9 @@ app.on(["GET", "POST"], ["/cmd/:name"], async c => { try { const mod = await loadCommandModule(cmd) if (!mod || !mod[method]) - return c.json({ error: `No ${method} export in ${cmd}` }, 500) + return c.json({ status: "error", output: `No ${method} export in ${cmd}` }, 500) - return c.json(await runInSession(sessionId, async () => { - const [status, output] = processExecOutput(await mod[method](c)) - return { status, output } - })) + return c.json(await runCommandFn(sessionId, async () => mod[method](c))) } catch (e: any) { return c.json({ status: "error", output: e.message || e.toString() }, 500) } diff --git a/src/shell.ts b/src/shell.ts index 34d0ca0..0122295 100644 --- a/src/shell.ts +++ b/src/shell.ts @@ -29,9 +29,20 @@ export async function runCommand(sessionId: string, taskId: string, input: strin return { status, output } } -export async function runInSession(sessionId: string, fn: () => Promise) { +export async function runCommandFn(sessionId: string, fn: () => Promise) { + let status: "ok" | "error" = "ok" + let output: CommandOutput = "" const state = getState(sessionId) - return await ALS.run(state, async () => await fn()) + + try { + const execOutput = await ALS.run(state, async () => await fn()) + ;[status, output] = processExecOutput(execOutput) + } catch (err) { + status = "error" + output = errorMessage(err) + } + + return { status, output } } async function exec(cmd: string, args: string[]): Promise<["ok" | "error", CommandOutput]> {