mirror of
https://github.com/github/codeql-action.git
synced 2026-04-17 20:43:17 +00:00
Improve error messages from CLI invocations
This commit is contained in:
committed by
Edoardo Pirovano
parent
db80a9a7c3
commit
40852fa52a
@@ -39,6 +39,16 @@ interface ExtraOptions {
|
||||
};
|
||||
}
|
||||
|
||||
export class CommandInvocationError extends Error {
|
||||
constructor(cmd: string, args: string[], exitCode: number, error: string) {
|
||||
super(
|
||||
`Failure invoking ${cmd} with arguments ${args}.\n
|
||||
Exit code ${exitCode} and error was:\n
|
||||
${error}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export interface CodeQL {
|
||||
/**
|
||||
* Get the path of the CodeQL executable.
|
||||
@@ -884,14 +894,35 @@ export function getExtraOptions(
|
||||
return all.concat(specific);
|
||||
}
|
||||
|
||||
/*
|
||||
* A constant defining the maximum number of characters we will keep from
|
||||
* the programs stderr for logging. This serves two purposes:
|
||||
* (1) It avoids an OOM if a program fails in a way that results it
|
||||
* printing many log lines.
|
||||
* (2) It avoids us hitting the limit of how much data we can send in our
|
||||
* status reports on GitHub.com.
|
||||
* This value was chosen because the maximum size of fields in the status
|
||||
* report is 100kb and UTF8 characters can be up to four bytes. 5,000
|
||||
* characters are left over to include other information.
|
||||
*/
|
||||
const maxErrorSize = 20_000;
|
||||
|
||||
async function runTool(cmd: string, args: string[] = []) {
|
||||
let output = "";
|
||||
await new toolrunner.ToolRunner(cmd, args, {
|
||||
let error = "";
|
||||
const exitCode = await new toolrunner.ToolRunner(cmd, args, {
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
output += data.toString();
|
||||
},
|
||||
stderr: (data: Buffer) => {
|
||||
const toRead = Math.min(maxErrorSize - error.length, data.length);
|
||||
error += data.toString("utf8", 0, toRead);
|
||||
},
|
||||
},
|
||||
ignoreReturnCode: true,
|
||||
}).exec();
|
||||
if (exitCode !== 0)
|
||||
throw new CommandInvocationError(cmd, args, exitCode, error);
|
||||
return output;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user