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