Skip to content

Commit

Permalink
fix: fix a bug that program couldn't print exception properly
Browse files Browse the repository at this point in the history
  • Loading branch information
async3619 committed Oct 4, 2022
1 parent 2845ce7 commit 8d32434
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/utils/runChallenge.ts
Expand Up @@ -33,14 +33,18 @@ async function runTestCase(
) {
try {
const data = await transpileAndRun(input, output, targetPath, provider);
if (data instanceof Error) {
throw data;
}

if (data[0] !== output) {
throw new TestCaseFailedError(index, data);
}
} catch (e) {
if (e instanceof TestCaseFailedError) {
throw e;
} else if (e instanceof Error) {
throw new TestCaseFailedError(index, (e as any).stderr || e.message, "Test case failed with an error");
throw new TestCaseFailedError(index, [(e as any).stderr || e.stack, ""], "Test case failed with an error");
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/utils/transpileAndRun.ts
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs-extra";
import * as esbuild from "esbuild";
import { NodeVM } from "vm2";

import logger from "../logger";
import { InputType } from "../types";
import { BaseProvider } from "../providers/base";

Expand All @@ -12,7 +13,7 @@ export async function transpileAndRun(
output: string,
targetPath: string,
provider: BaseProvider,
): Promise<[string, string]> {
): Promise<[string, string] | Error> {
let fileContent = await fs.readFile(targetPath).then(res => res.toString());
fileContent = provider.beforeExecute(fileContent, input);

Expand Down Expand Up @@ -51,7 +52,17 @@ export async function transpileAndRun(
vm.on("console.warn", handleConsoleMessage(outputBuffer));
vm.on("console.error", handleConsoleMessage(outputBuffer));
vm.on("console.debug", handleConsoleMessage(debugBuffer));
vm.run(transpiledContent.code);

try {
vm.run(transpiledContent.code);
} catch (e) {
if (!(e instanceof Error)) {
logger.error("fatal error: " + e);
process.exit();
}

return e;
}

return [outputBuffer.join("\n").replace(/\r\n/g, "\n"), debugBuffer.join("\n").replace(/\r\n/g, "\n")];
}

0 comments on commit 8d32434

Please sign in to comment.