Refactoring: Simplify retrieving error message

This commit is contained in:
Henry Mercer
2024-09-16 22:38:35 +02:00
parent bbd7c801a0
commit dd7307d603
54 changed files with 106 additions and 89 deletions

View File

@@ -7,7 +7,7 @@ import * as core from "@actions/core";
import * as debugArtifacts from "./debug-artifacts";
import { EnvVar } from "./environment";
import { wrapError } from "./util";
import { getErrorMessage } from "./util";
async function runWrapper() {
try {
@@ -18,7 +18,7 @@ async function runWrapper() {
}
} catch (error) {
core.setFailed(
`analyze post-action step failed: ${wrapError(error).message}`,
`analyze post-action step failed: ${getErrorMessage(error)}`,
);
}
}

View File

@@ -417,7 +417,7 @@ async function runWrapper() {
try {
await runPromise;
} catch (error) {
core.setFailed(`analyze action failed: ${util.wrapError(error).message}`);
core.setFailed(`analyze action failed: ${util.getErrorMessage(error)}`);
}
await util.checkForTimeout();
}

View File

@@ -314,7 +314,7 @@ export async function runQueries(
statusReport.analyze_failure_language = language;
throw new CodeQLAnalysisError(
statusReport,
`Error running analysis for ${language}: ${util.wrapError(e).message}`,
`Error running analysis for ${language}: ${util.getErrorMessage(e)}`,
util.wrapError(e),
);
}

View File

@@ -24,6 +24,7 @@ import {
checkActionVersion,
checkDiskUsage,
checkGitHubVersionInRange,
getErrorMessage,
initializeEnvironment,
wrapError,
} from "./util";
@@ -141,7 +142,7 @@ async function runWrapper() {
try {
await run();
} catch (error) {
core.setFailed(`autobuild action failed. ${wrapError(error).message}`);
core.setFailed(`autobuild action failed. ${getErrorMessage(error)}`);
}
}

View File

@@ -31,7 +31,7 @@ import * as setupCodeql from "./setup-codeql";
import { ToolsFeature, isSupportedToolsFeature } from "./tools-features";
import { shouldEnableIndirectTracing } from "./tracer-config";
import * as util from "./util";
import { BuildMode, wrapError, cloneObject } from "./util";
import { BuildMode, cloneObject, getErrorMessage } from "./util";
type Options = Array<string | number | boolean>;
@@ -398,7 +398,7 @@ export async function setupCodeQL(
};
} catch (e) {
throw new Error(
`Unable to download and extract CodeQL CLI: ${wrapError(e).message}`,
`Unable to download and extract CodeQL CLI: ${getErrorMessage(e)}`,
);
}
}
@@ -707,7 +707,7 @@ export async function getCodeQLForCmd(
e instanceof util.ConfigurationError
? util.ConfigurationError
: Error;
throw new ErrorConstructor(`${prefix} ${util.wrapError(e).message}`);
throw new ErrorConstructor(`${prefix} ${getErrorMessage(e)}`);
} else {
throw e;
}

View File

@@ -17,8 +17,8 @@ import {
bundleDb,
doesDirectoryExist,
getCodeQLDatabasePath,
getErrorMessage,
listFolder,
wrapError,
} from "./util";
export function sanitizeArifactName(name: string): string {
@@ -94,9 +94,9 @@ function tryGetSarifResultPath(
}
} catch (e) {
logger.warning(
`Failed to find SARIF results path for ${language}. Reason: ${
wrapError(e).message
}`,
`Failed to find SARIF results path for ${language}. Reason: ${getErrorMessage(
e,
)}`,
);
}
return [];
@@ -114,16 +114,16 @@ async function tryBundleDatabase(
} catch (e) {
logger.warning(
`Failed to bundle database for ${language} using the CLI. ` +
`Falling back to a partial bundle. Reason: ${wrapError(e).message}`,
`Falling back to a partial bundle. Reason: ${getErrorMessage(e)}`,
);
}
}
return [await createPartialDatabaseBundle(config, language)];
} catch (e) {
logger.warning(
`Failed to bundle database for ${language}. Reason: ${
wrapError(e).message
}`,
`Failed to bundle database for ${language}. Reason: ${getErrorMessage(
e,
)}`,
);
return [];
}
@@ -168,7 +168,7 @@ export async function uploadAllAvailableDebugArtifacts(
);
} catch (e) {
logger.warning(
`Failed to upload debug artifacts. Reason: ${wrapError(e).message}`,
`Failed to upload debug artifacts. Reason: ${getErrorMessage(e)}`,
);
}
}
@@ -210,7 +210,7 @@ export async function uploadDebugArtifacts(
} catch (e) {
// A failure to upload debug artifacts should not fail the entire action.
core.warning(
`Failed to upload debug artifacts. Reason: ${wrapError(e).message}`,
`Failed to upload debug artifacts. Reason: ${getErrorMessage(e)}`,
);
}
}

View File

@@ -62,6 +62,7 @@ import {
wrapError,
checkActionVersion,
cloneObject,
getErrorMessage,
} from "./util";
import { validateWorkflow } from "./workflow";
@@ -700,7 +701,7 @@ async function runWrapper() {
try {
await run();
} catch (error) {
core.setFailed(`init action failed: ${wrapError(error).message}`);
core.setFailed(`init action failed: ${getErrorMessage(error)}`);
}
await checkForTimeout();
}

View File

@@ -177,17 +177,15 @@ export function cleanupDatabaseClusterDirectory(
if (isSelfHostedRunner()) {
throw new util.ConfigurationError(
`${blurb} This can happen if another process is using the directory or the directory is owned by a different user. ` +
`Please clean up the directory manually and rerun the job. Details: ${
util.wrapError(e).message
}`,
`Please clean up the directory manually and rerun the job. Details: ${util.getErrorMessage(
e,
)}`,
);
} else {
throw new Error(
`${blurb} This shouldn't typically happen on hosted runners. ` +
"If you are using an advanced setup, please check your workflow, otherwise we " +
`recommend rerunning the job. Details: ${
util.wrapError(e).message
}`,
`recommend rerunning the job. Details: ${util.getErrorMessage(e)}`,
);
}
}

View File

@@ -22,6 +22,7 @@ import {
checkDiskUsage,
checkForTimeout,
checkGitHubVersionInRange,
getErrorMessage,
wrapError,
} from "./util";
@@ -117,9 +118,9 @@ async function runWrapper() {
await run();
} catch (error) {
core.setFailed(
`${ActionName.ResolveEnvironment} action failed: ${
wrapError(error).message
}`,
`${ActionName.ResolveEnvironment} action failed: ${getErrorMessage(
error,
)}`,
);
}
await checkForTimeout();

View File

@@ -17,10 +17,10 @@ import {
setupTests,
} from "./testing-utils";
import {
getErrorMessage,
GitHubVariant,
initializeEnvironment,
withTmpDir,
wrapError,
} from "./util";
setupTests(test);
@@ -56,7 +56,7 @@ test("convert to semver", (t) => {
);
t.deepEqual(parsedVersion, expectedVersion);
} catch (e) {
t.fail(wrapError(e).message);
t.fail(getErrorMessage(e));
}
}
});

View File

@@ -8,7 +8,7 @@ import * as core from "@actions/core";
import * as actionsUtil from "./actions-util";
import * as configUtils from "./config-utils";
import { wrapError } from "./util";
import { getErrorMessage } from "./util";
async function runWrapper() {
try {
@@ -18,7 +18,7 @@ async function runWrapper() {
}
} catch (error) {
core.setFailed(
`start-proxy post-action step failed: ${wrapError(error).message}`,
`start-proxy post-action step failed: ${getErrorMessage(error)}`,
);
}
const config = await configUtils.getConfig(

View File

@@ -169,9 +169,7 @@ async function startProxy(
core.setOutput("proxy_port", port.toString());
core.setOutput("proxy_ca_certificate", config.ca.cert);
} catch (error) {
core.setFailed(
`start-proxy action failed: ${util.wrapError(error).message}`,
);
core.setFailed(`start-proxy action failed: ${util.getErrorMessage(error)}`);
}
}

View File

@@ -26,7 +26,7 @@ import {
DiskUsage,
assertNever,
BuildMode,
wrapError,
getErrorMessage,
} from "./util";
export enum ActionName {
@@ -440,9 +440,9 @@ export async function sendStatusReport<S extends StatusReportBase>(
// something else has gone wrong and the request/response will be logged by octokit
// it's possible this is a transient error and we should continue scanning
core.warning(
`An unexpected error occurred when sending code scanning status report: ${
wrapError(e).message
}`,
`An unexpected error occurred when sending code scanning status report: ${getErrorMessage(
e,
)}`,
);
}
}

View File

@@ -11,7 +11,12 @@ import { DocUrl } from "./doc-url";
import { Feature, FeatureEnablement } from "./feature-flags";
import { Language } from "./languages";
import { Logger } from "./logging";
import { isHTTPError, tryGetFolderBytes, withTimeout, wrapError } from "./util";
import {
getErrorMessage,
isHTTPError,
tryGetFolderBytes,
withTimeout,
} from "./util";
// This constant should be bumped if we make a breaking change
// to how the CodeQL Action stores or retrieves the TRAP cache,
@@ -239,7 +244,7 @@ export async function cleanupTrapCaches(
} else {
logger.info(`Failed to cleanup TRAP caches, continuing. Details: ${e}`);
}
return { trap_cache_cleanup_error: wrapError(e).message };
return { trap_cache_cleanup_error: getErrorMessage(e) };
}
}

View File

@@ -24,12 +24,12 @@ import { ToolsFeature } from "./tools-features";
import * as util from "./util";
import {
ConfigurationError,
getErrorMessage,
getRequiredEnvParam,
GitHubVariant,
GitHubVersion,
SarifFile,
SarifRun,
wrapError,
} from "./util";
const GENERIC_403_MSG =
@@ -440,7 +440,7 @@ export function validateSarifFileSchema(sarifFilePath: string, logger: Logger) {
sarif = JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile;
} catch (e) {
throw new InvalidSarifUploadError(
`Invalid SARIF. JSON syntax error: ${wrapError(e).message}`,
`Invalid SARIF. JSON syntax error: ${getErrorMessage(e)}`,
);
}
// eslint-disable-next-line @typescript-eslint/no-require-imports

View File

@@ -7,7 +7,7 @@ import * as core from "@actions/core";
import * as debugArtifacts from "./debug-artifacts";
import { EnvVar } from "./environment";
import { wrapError } from "./util";
import { getErrorMessage } from "./util";
async function runWrapper() {
try {
@@ -18,7 +18,7 @@ async function runWrapper() {
}
} catch (error) {
core.setFailed(
`upload-sarif post-action step failed: ${wrapError(error).message}`,
`upload-sarif post-action step failed: ${getErrorMessage(error)}`,
);
}
}

View File

@@ -19,6 +19,7 @@ import {
ConfigurationError,
checkActionVersion,
checkDiskUsage,
getErrorMessage,
getRequiredEnvParam,
initializeEnvironment,
isInTestMode,
@@ -133,7 +134,7 @@ async function runWrapper() {
await run();
} catch (error) {
core.setFailed(
`codeql/upload-sarif action failed: ${wrapError(error).message}`,
`codeql/upload-sarif action failed: ${getErrorMessage(error)}`,
);
}
}

View File

@@ -998,8 +998,14 @@ export function wrapError(error: unknown): Error {
return error instanceof Error ? error : new Error(String(error));
}
/**
* Returns an appropriate message for the error.
*
* If the error is an `Error` instance, this returns the error message without
* an `Error: ` prefix.
*/
export function getErrorMessage(error: unknown): string {
return error instanceof Error ? error.toString() : String(error);
return error instanceof Error ? error.message : String(error);
}
export function prettyPrintPack(pack: Pack) {