From c9af422461bf77adc424b6ecde3f1d7fd2bb9c16 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Thu, 26 Aug 2021 18:46:04 -0700 Subject: [PATCH] Catch more errors --- backend/api.js | 120 +++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/backend/api.js b/backend/api.js index 0eb1834..846c745 100644 --- a/backend/api.js +++ b/backend/api.js @@ -417,69 +417,81 @@ export class Session { }; stopLSP = async () => { - if (this.lsp) { - this.lsp.stopping = true; - this.lsp.proc.kill(); - this.lsp = null; + try { + if (this.lsp) { + this.lsp.stopping = true; + this.lsp.proc.kill(); + this.lsp = null; + } + } catch (err) { + logError(err); } }; startLSP = async () => { - if (this.config.lsp) { - await this.stopLSP(); - if (this.config.lsp.setup) { - await this.run(this.privilegedExec(this.config.lsp.setup)); + try { + if (this.config.lsp) { + await this.stopLSP(); + if (this.config.lsp.setup) { + await this.run(this.privilegedExec(this.config.lsp.setup)); + } + const lspArgs = this.privilegedExec(this.config.lsp.start); + const lspProc = spawn(lspArgs[0], lspArgs.slice(1)); + const lsp = { + proc: lspProc, + reader: new rpc.StreamMessageReader(lspProc.stdout), + writer: new rpc.StreamMessageWriter(lspProc.stdin), + live: true, + stopping: false, + }; + this.lsp = lsp; + this.lsp.reader.listen((data) => { + this.send({ event: "lspOutput", output: data }); + }); + lspProc.stderr.on("data", (data) => { + if (lsp.live) { + this.send({ + event: "serviceLog", + service: "lsp", + output: data.toString("utf8"), + }); + } + }); + lspProc.on("close", (code, signal) => { + if (lsp.stopping) { + this.send({ + event: "lspStopped", + }); + } else { + this.send({ + event: "serviceFailed", + service: "lsp", + error: `Exited with status ${signal || code}`, + code: signal || code, + }); + } + }); + lspProc.on("error", (err) => + this.send({ event: "serviceFailed", service: "lsp", error: `${err}` }) + ); + this.send({ event: "lspStarted", root: this.homedir }); } - const lspArgs = this.privilegedExec(this.config.lsp.start); - const lspProc = spawn(lspArgs[0], lspArgs.slice(1)); - const lsp = { - proc: lspProc, - reader: new rpc.StreamMessageReader(lspProc.stdout), - writer: new rpc.StreamMessageWriter(lspProc.stdin), - live: true, - stopping: false, - }; - this.lsp = lsp; - this.lsp.reader.listen((data) => { - this.send({ event: "lspOutput", output: data }); - }); - lspProc.stderr.on("data", (data) => { - if (lsp.live) { - this.send({ - event: "serviceLog", - service: "lsp", - output: data.toString("utf8"), - }); - } - }); - lspProc.on("close", (code, signal) => { - if (lsp.stopping) { - this.send({ - event: "lspStopped", - }); - } else { - this.send({ - event: "serviceFailed", - service: "lsp", - error: `Exited with status ${signal || code}`, - code: signal || code, - }); - } - }); - lspProc.on("error", (err) => - this.send({ event: "serviceFailed", service: "lsp", error: `${err}` }) - ); - this.send({ event: "lspStarted", root: this.homedir }); + } catch (err) { + logError(err); } }; ensure = async (cmd) => { - const code = ( - await this.run(this.privilegedExec(cmd), { - check: false, - }) - ).code; - this.send({ event: "ensured", code }); + try { + const code = ( + await this.run(this.privilegedExec(cmd), { + check: false, + }) + ).code; + this.send({ event: "ensured", code }); + } catch (err) { + logError(err); + } }; teardown = async () => {