Surface fatal CLI errors in interpret-results and run-queries (#1407)

Co-authored-by: Henry Mercer <henry.mercer@me.com>
This commit is contained in:
Angela P Wen 2022-12-02 14:05:21 +01:00 committed by GitHub
parent 0d9b15ca93
commit aa0e650c6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 137 additions and 85 deletions

View file

@ -31,7 +31,7 @@ const safeWhich = __importStar(require("@chrisgavin/safe-which"));
* @param args optional arguments for tool. Escaping is handled by the lib.
* @param matchers defines specific codes and/or regexes that should lead to return of a custom error
* @param options optional exec options. See ExecOptions
* @returns Promise<number> exit code
* @returns ReturnState exit code and stdout output, if applicable
*/
async function toolrunnerErrorCatcher(commandLine, args, matchers, options) {
var _a, _b;
@ -54,40 +54,36 @@ async function toolrunnerErrorCatcher(commandLine, args, matchers, options) {
},
};
// we capture the original return code or error so that if no match is found we can duplicate the behavior
let returnState;
let exitCode;
try {
returnState = await new toolrunner.ToolRunner(await safeWhich.safeWhich(commandLine), args, {
exitCode = await new toolrunner.ToolRunner(await safeWhich.safeWhich(commandLine), args, {
...options,
listeners,
ignoreReturnCode: true, // so we can check for specific codes using the matchers
}).exec();
}
catch (e) {
returnState = e instanceof Error ? e : new Error(String(e));
}
// if there is a zero return code then we do not apply the matchers
if (returnState === 0)
return returnState;
if (matchers) {
for (const matcher of matchers) {
if (matcher.exitCode === returnState ||
((_a = matcher.outputRegex) === null || _a === void 0 ? void 0 : _a.test(stderr)) ||
((_b = matcher.outputRegex) === null || _b === void 0 ? void 0 : _b.test(stdout))) {
throw new Error(matcher.message);
// if there is a zero return code then we do not apply the matchers
if (exitCode === 0)
return { exitCode, stdout };
if (matchers) {
for (const matcher of matchers) {
if (matcher.exitCode === exitCode ||
((_a = matcher.outputRegex) === null || _a === void 0 ? void 0 : _a.test(stderr)) ||
((_b = matcher.outputRegex) === null || _b === void 0 ? void 0 : _b.test(stdout))) {
throw new Error(matcher.message);
}
}
}
}
if (typeof returnState === "number") {
// only if we were instructed to ignore the return code do we ever return it non-zero
if (options === null || options === void 0 ? void 0 : options.ignoreReturnCode) {
return returnState;
return { exitCode, stdout };
}
else {
throw new Error(`The process '${commandLine}' failed with exit code ${returnState}`);
throw new Error(`The process '${commandLine}' failed with exit code ${exitCode}`);
}
}
else {
throw returnState;
catch (e) {
const error = e instanceof Error ? e : new Error(String(e));
throw error;
}
}
exports.toolrunnerErrorCatcher = toolrunnerErrorCatcher;