More error handling

This commit is contained in:
Radon Rosborough 2020-07-20 06:31:14 -06:00
parent 89fa4b58cc
commit 5c28ca4fba
1 changed files with 75 additions and 64 deletions

View File

@ -259,6 +259,26 @@ export class Session {
} }
}; };
writeCode = async (code: string) => {
if (this.config.main.includes("/")) {
await this.run(
this.privilegedSpawn([
"mkdir",
"-p",
path.dirname(`${this.homedir}/${this.config.main}`),
])
);
}
await this.run(
this.privilegedSpawn([
"sh",
"-c",
`cat > ${path.resolve(this.homedir, this.config.main)}`,
]),
{ input: code }
);
};
runCode = async (code?: string) => { runCode = async (code?: string) => {
try { try {
const { const {
@ -299,23 +319,7 @@ export class Session {
if (code && suffix) { if (code && suffix) {
code += suffix; code += suffix;
} }
if (main.includes("/")) { await this.writeCode(code);
await this.run(
this.privilegedSpawn([
"mkdir",
"-p",
path.dirname(`${this.homedir}/${main}`),
])
);
}
await this.run(
this.privilegedSpawn([
"sh",
"-c",
`cat > ${path.resolve(this.homedir, main)}`,
]),
{ input: code }
);
const termArgs = this.privilegedSpawn(bash(cmdline)); const termArgs = this.privilegedSpawn(bash(cmdline));
const term = { const term = {
pty: pty.spawn(termArgs[0], termArgs.slice(1), { pty: pty.spawn(termArgs[0], termArgs.slice(1), {
@ -339,6 +343,7 @@ export class Session {
}; };
formatCode = async (code: string) => { formatCode = async (code: string) => {
try {
if (!this.config.format) { if (!this.config.format) {
this.log("formatCode ignored because format is null"); this.log("formatCode ignored because format is null");
return; return;
@ -352,6 +357,7 @@ export class Session {
this.formatter.live = false; this.formatter.live = false;
this.formatter = null; this.formatter = null;
} }
await this.writeCode(code);
const args = this.privilegedSpawn(bash(this.config.format)); const args = this.privilegedSpawn(bash(this.config.format));
const formatter = { const formatter = {
proc: spawn(args[0], args.slice(1)), proc: spawn(args[0], args.slice(1)),
@ -360,20 +366,19 @@ export class Session {
output: "", output: "",
}; };
formatter.proc.stdout!.on("data", (data) => { formatter.proc.stdout!.on("data", (data) => {
if (formatter.live) { if (!formatter.live) return;
formatter.output += data.toString("utf8"); formatter.output += data.toString("utf8");
}
}); });
formatter.proc.stderr!.on("data", (data) => { formatter.proc.stderr!.on("data", (data) => {
if (formatter.live) { if (!formatter.live) return;
this.send({ this.send({
event: "serviceLog", event: "serviceLog",
service: "formatter", service: "formatter",
output: data.toString("utf8"), output: data.toString("utf8"),
}); });
}
}); });
formatter.proc.on("exit", (code, signal) => { formatter.proc.on("exit", (code, signal) => {
if (!formatter.live) return;
if (code === 0) { if (code === 0) {
this.send({ this.send({
event: "formattedCode", event: "formattedCode",
@ -388,14 +393,20 @@ export class Session {
}); });
} }
}); });
formatter.proc.on("error", (err) => formatter.proc.on("error", (err) => {
if (!formatter.live) return;
this.send({ this.send({
event: "serviceFailed", event: "serviceFailed",
service: "formatter", service: "formatter",
error: `${err}`, error: `${err}`,
}) });
); });
this.formatter = formatter; this.formatter = formatter;
} catch (err) {
this.log(`Error while running code formatter`);
console.log(err);
this.sendError(err);
}
}; };
teardown = async () => { teardown = async () => {