More error handling
This commit is contained in:
parent
89fa4b58cc
commit
5c28ca4fba
|
@ -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,63 +343,70 @@ export class Session {
|
||||||
};
|
};
|
||||||
|
|
||||||
formatCode = async (code: string) => {
|
formatCode = async (code: string) => {
|
||||||
if (!this.config.format) {
|
try {
|
||||||
this.log("formatCode ignored because format is null");
|
if (!this.config.format) {
|
||||||
return;
|
this.log("formatCode ignored because format is null");
|
||||||
}
|
return;
|
||||||
if (this.formatter) {
|
|
||||||
const pid = this.formatter.proc.pid;
|
|
||||||
const args = this.privilegedSpawn(
|
|
||||||
bash(`kill -SIGTERM ${pid}; sleep 1; kill -SIGKILL ${pid}`)
|
|
||||||
);
|
|
||||||
spawn(args[0], args.slice(1));
|
|
||||||
this.formatter.live = false;
|
|
||||||
this.formatter = null;
|
|
||||||
}
|
|
||||||
const args = this.privilegedSpawn(bash(this.config.format));
|
|
||||||
const formatter = {
|
|
||||||
proc: spawn(args[0], args.slice(1)),
|
|
||||||
live: true,
|
|
||||||
input: code,
|
|
||||||
output: "",
|
|
||||||
};
|
|
||||||
formatter.proc.stdout!.on("data", (data) => {
|
|
||||||
if (formatter.live) {
|
|
||||||
formatter.output += data.toString("utf8");
|
|
||||||
}
|
}
|
||||||
});
|
if (this.formatter) {
|
||||||
formatter.proc.stderr!.on("data", (data) => {
|
const pid = this.formatter.proc.pid;
|
||||||
if (formatter.live) {
|
const args = this.privilegedSpawn(
|
||||||
|
bash(`kill -SIGTERM ${pid}; sleep 1; kill -SIGKILL ${pid}`)
|
||||||
|
);
|
||||||
|
spawn(args[0], args.slice(1));
|
||||||
|
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)),
|
||||||
|
live: true,
|
||||||
|
input: code,
|
||||||
|
output: "",
|
||||||
|
};
|
||||||
|
formatter.proc.stdout!.on("data", (data) => {
|
||||||
|
if (!formatter.live) return;
|
||||||
|
formatter.output += data.toString("utf8");
|
||||||
|
});
|
||||||
|
formatter.proc.stderr!.on("data", (data) => {
|
||||||
|
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",
|
||||||
code: formatter.output,
|
code: formatter.output,
|
||||||
originalCode: formatter.input,
|
originalCode: formatter.input,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
this.send({
|
||||||
|
event: "serviceFailed",
|
||||||
|
service: "formatter",
|
||||||
|
error: `Exited with status ${signal || code}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
formatter.proc.on("error", (err) => {
|
||||||
|
if (!formatter.live) return;
|
||||||
this.send({
|
this.send({
|
||||||
event: "serviceFailed",
|
event: "serviceFailed",
|
||||||
service: "formatter",
|
service: "formatter",
|
||||||
error: `Exited with status ${signal || code}`,
|
error: `${err}`,
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
});
|
this.formatter = formatter;
|
||||||
formatter.proc.on("error", (err) =>
|
} catch (err) {
|
||||||
this.send({
|
this.log(`Error while running code formatter`);
|
||||||
event: "serviceFailed",
|
console.log(err);
|
||||||
service: "formatter",
|
this.sendError(err);
|
||||||
error: `${err}`,
|
}
|
||||||
})
|
|
||||||
);
|
|
||||||
this.formatter = formatter;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
teardown = async () => {
|
teardown = async () => {
|
||||||
|
|
Loading…
Reference in New Issue