Skip to content

Commit 12e5ce4

Browse files
authored
fix(batch): handle compile errors in batch mode (#794)
1 parent 9cbeef8 commit 12e5ce4

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/commands/buildJs.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import { strict as assert } from "assert";
12
import type { CleanupOptions } from "faastjs";
3+
import { FaastError } from "faastjs";
24
import { promises as fs } from "fs";
35
import pSettled from "p-settle";
46
import path from "path";
57
import recursive from "recursive-readdir";
68
import { assertString } from "../assert.js";
79
import { resultInfoLogType } from "../cli.js";
8-
import type { CompilerError } from "../compiler.js";
10+
import type * as compilerCoreFunctions from "../compiler-core.js";
911
import {
12+
CompilerError,
1013
compileToJson,
1114
createCompilerOptionsForChunks,
1215
createCompilerOptionsForPage,
1316
} from "../compiler.js";
14-
import type * as compilerCoreFunctions from "../compiler-core.js";
1517
import type { DuckConfig } from "../duckconfig.js";
1618
import type { EntryConfig } from "../entryconfig.js";
1719
import { loadEntryConfig } from "../entryconfig.js";
@@ -147,7 +149,7 @@ async function waitAllAndThrowIfAnyCompilationsFailed(
147149
};
148150
}
149151
// has some errors
150-
const reason = result.reason as CompilerError;
152+
const reason = result.reason as any;
151153
try {
152154
const { command, items } = parseErrorReason(reason, config);
153155
return {
@@ -167,11 +169,25 @@ async function waitAllAndThrowIfAnyCompilationsFailed(
167169
return reasons;
168170

169171
function parseErrorReason(
170-
reason: CompilerError & { info?: Record<string, any> },
172+
reason: any,
171173
config: DuckConfig
172174
): { command: string; items: CompileErrorItem[] } {
173-
const { message: stderr, info } = reason;
174-
const message = config.batch && info ? info.message : stderr;
175+
let message: string;
176+
if (config.batch) {
177+
assert(reason instanceof FaastError);
178+
assert.equal(reason.name, "CompilerError");
179+
// In batch mode, faast.js surrounds an error with a FaastError that
180+
// is a subclass of VError. `.jse_shortmsg` is a property of VError for
181+
// debug. The original error message is only stored this prop.
182+
// `.message` is transformed into a string concatenated with `cause`,
183+
// so it cannot be used.
184+
// https://github.com/TritonDataCenter/node-verror/blob/v1.10.1/lib/verror.js#L167-L172
185+
message = (reason as any).jse_shortmsg;
186+
} else if (reason instanceof CompilerError) {
187+
message = reason.message;
188+
} else {
189+
throw new TypeError(`reason is an unexpected error`);
190+
}
175191
const [command, , ...messages] = message.split("\n");
176192
return { command, items: JSON.parse(messages.join("\n")) };
177193
}

0 commit comments

Comments
 (0)