Skip to content

Commit

Permalink
Capture the details of fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Mercer committed Jul 21, 2023
1 parent 76b2afa commit 21c9267
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 371 deletions.
34 changes: 26 additions & 8 deletions lib/codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/codeql.js.map

Large diffs are not rendered by default.

59 changes: 36 additions & 23 deletions src/codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { getOptionalInput, isAnalyzingDefaultBranch } from "./actions-util";
import * as api from "./api-client";
import type { Config } from "./config-utils";
import { EnvVar } from "./environment";
import { errorMatchers } from "./error-matcher";
import {
CODEQL_VERSION_NEW_ANALYSIS_SUMMARY,
CodeQLDefaultVersionInfo,
Expand All @@ -20,7 +19,6 @@ import {
import { isTracedLanguage, Language } from "./languages";
import { Logger } from "./logging";
import * as setupCodeql from "./setup-codeql";
import { toolrunnerErrorCatcher } from "./toolrunner-error-catcher";
import * as util from "./util";
import { wrapError } from "./util";

Expand Down Expand Up @@ -627,19 +625,15 @@ export async function getCodeQLForCmd(
`autobuild${ext}`
);
// Run trace command
await toolrunnerErrorCatcher(
cmd,
[
"database",
"trace-command",
...(await getTrapCachingExtractorConfigArgsForLang(config, language)),
...getExtraOptionsFromEnv(["database", "trace-command"]),
databasePath,
"--",
traceCommand,
],
errorMatchers
);
await runTool(cmd, [
"database",
"trace-command",
...(await getTrapCachingExtractorConfigArgsForLang(config, language)),
...getExtraOptionsFromEnv(["database", "trace-command"]),
databasePath,
"--",
traceCommand,
]);
},
async finalizeDatabase(
databasePath: string,
Expand Down Expand Up @@ -782,7 +776,7 @@ export async function getCodeQLForCmd(
if (querySuitePath) {
codeqlArgs.push(querySuitePath);
}
await toolrunnerErrorCatcher(cmd, codeqlArgs, errorMatchers);
await runTool(cmd, codeqlArgs);
},
async databaseInterpretResults(
databasePath: string,
Expand Down Expand Up @@ -848,17 +842,13 @@ export async function getCodeQLForCmd(
codeqlArgs.push(...querySuitePaths);
}
// capture stdout, which contains analysis summaries
const returnState = await toolrunnerErrorCatcher(
cmd,
codeqlArgs,
errorMatchers
);
const returnState = await runTool(cmd, codeqlArgs);

if (shouldWorkaroundInvalidNotifications) {
util.fixInvalidNotificationsInFile(codeqlOutputFile, sarifFile, logger);
}

return returnState.stdout;
return returnState;
},
async databasePrintBaseline(databasePath: string): Promise<string> {
const codeqlArgs = [
Expand Down Expand Up @@ -1161,11 +1151,34 @@ async function runTool(
ignoreReturnCode: true,
...(opts.stdin ? { input: Buffer.from(opts.stdin || "") } : {}),
}).exec();
if (exitCode !== 0)
if (exitCode !== 0) {
error = extractFatalErrors(error) || error;
throw new CommandInvocationError(cmd, args, exitCode, error, output);
}
return output;
}

function extractFatalErrors(error: string): string | undefined {
const fatalErrors: string[] = [];
const fatalErrorRegex = /.*fatal error occurred:/gi;
let lastFatalErrorIndex: number | undefined;
let match: RegExpMatchArray | null;
while ((match = fatalErrorRegex.exec(error)) !== null) {
if (lastFatalErrorIndex !== undefined) {
fatalErrors.push(error.slice(lastFatalErrorIndex, match.index));
}
lastFatalErrorIndex = match.index;
}
if (lastFatalErrorIndex !== undefined) {
const lastError = error.slice(lastFatalErrorIndex);
return (
lastError +
(fatalErrors.length > 0 ? `\nContext:\n${fatalErrors.join("\n")}` : "")
);
}
return undefined;
}

/**
* If appropriate, generates a code scanning configuration that is to be used for a scan.
* If the configuration is not to be generated, returns undefined.
Expand Down
27 changes: 0 additions & 27 deletions src/error-matcher.test.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/error-matcher.ts

This file was deleted.

Loading

0 comments on commit 21c9267

Please sign in to comment.