diff --git a/lib/entry-points.js b/lib/entry-points.js index 3fbacc32e4..ed83519d07 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -145995,7 +145995,13 @@ function parseMatrixInput(matrixInput) { if (matrixInput === void 0 || matrixInput === "null") { return void 0; } - return JSON.parse(matrixInput); + try { + return JSON.parse(matrixInput); + } catch (err) { + throw new Error( + `Failed to parse matrix input '${matrixInput}': ${getErrorMessage(err)}` + ); + } } function wrapError(error3) { return error3 instanceof Error ? error3 : new Error(String(error3)); @@ -146063,7 +146069,11 @@ var BuildMode = /* @__PURE__ */ ((BuildMode3) => { return BuildMode3; })(BuildMode || {}); function cloneObject(obj) { - return JSON.parse(JSON.stringify(obj)); + try { + return JSON.parse(JSON.stringify(obj)); + } catch (err) { + throw new Error(`Cloning object failed: ${getErrorMessage(err)}`); + } } async function cleanUpPath(file, name, logger) { logger.debug(`Cleaning up ${name}.`); @@ -146389,11 +146399,16 @@ var persistInputs = function(env = getEnv()) { core3.saveState(persistedInputsKey, JSON.stringify(inputEnvironmentVariables)); }; var restoreInputs = function() { - const persistedInputs = core3.getState(persistedInputsKey); - if (persistedInputs) { - for (const [name, value] of JSON.parse(persistedInputs)) { - process.env[name] = value; + try { + const persistedInputsValue = core3.getState(persistedInputsKey); + if (persistedInputsValue) { + const persistedInputs = JSON.parse(persistedInputsValue); + for (const [name, value] of persistedInputs) { + process.env[name] = value; + } } + } catch (err) { + throw new Error(`Unable to restore inputs: ${getErrorMessage(err)}`); } }; function getPullRequestBranches(env = getEnv()) { @@ -152816,18 +152831,20 @@ async function endTracingForCluster(codeql, config, logger) { } } async function getTracerConfigForCluster(config) { - const tracingEnvVariables = JSON.parse( - fs15.readFileSync( - path14.resolve( - config.dbLocation, - "temp/tracingEnvironment/start-tracing.json" - ), - "utf8" - ) + const filePath = path14.resolve( + config.dbLocation, + "temp/tracingEnvironment/start-tracing.json" ); - return { - env: tracingEnvVariables - }; + try { + const tracingEnvVariables = JSON.parse(fs15.readFileSync(filePath, "utf8")); + return { + env: tracingEnvVariables + }; + } catch (err) { + throw new Error( + `Failed to parse tracing environment from '${filePath}': ${getErrorMessage(err)}` + ); + } } async function getCombinedTracerConfig(codeql, config) { if (!await shouldEnableIndirectTracing(codeql, config)) { @@ -153261,7 +153278,14 @@ async function getCodeQLForCmd(logger, cmd, checkVersion) { } } ).exec(); - return JSON.parse(extractorPath); + try { + return JSON.parse(extractorPath); + } catch (err) { + throw new Error( + `Failed to parse extractor path for '${language}' from CLI: ${getErrorMessage(err)} +Output was: ${extractorPath}` + ); + } }, async resolveQueriesStartingPacks(queries) { const codeqlArgs = [ @@ -155734,7 +155758,13 @@ function getToolNames(sarifFile) { return Object.keys(toolNames); } function readSarifFile(sarifFilePath) { - return JSON.parse(fs21.readFileSync(sarifFilePath, "utf8")); + try { + return JSON.parse(fs21.readFileSync(sarifFilePath, "utf8")); + } catch (err) { + throw new Error( + `Parsing SARIF file at '${sarifFilePath}' failed: ${getErrorMessage(err)}` + ); + } } function combineSarifFiles(sarifFiles, logger) { logger.info(`Loading SARIF file(s)`); @@ -158879,7 +158909,7 @@ var import_async = __toESM(require_async(), 1); var import_path7 = require("path"); // node_modules/archiver/lib/error.js -var import_util35 = __toESM(require("util"), 1); +var import_util36 = __toESM(require("util"), 1); var ERROR_CODES = { ABORTED: "archive was aborted", DIRECTORYDIRPATHREQUIRED: "diretory dirpath argument must be a non-empty string value", @@ -158904,7 +158934,7 @@ function ArchiverError(code, data) { this.code = code; this.data = data; } -import_util35.default.inherits(ArchiverError, Error); +import_util36.default.inherits(ArchiverError, Error); // node_modules/archiver/lib/core.js var import_readable_stream2 = __toESM(require_ours(), 1); diff --git a/src/actions-util.ts b/src/actions-util.ts index 677bb04b1b..dd11f7aa0d 100644 --- a/src/actions-util.ts +++ b/src/actions-util.ts @@ -14,6 +14,7 @@ import { getCodeQLDatabasePath, ConfigurationError, getEnv, + getErrorMessage, } from "./util"; /** @@ -413,11 +414,17 @@ export const persistInputs = function (env: Env = getEnv()) { * Restores all inputs to the action from the persisted state. */ export const restoreInputs = function () { - const persistedInputs = core.getState(persistedInputsKey); - if (persistedInputs) { - for (const [name, value] of JSON.parse(persistedInputs)) { - process.env[name] = value; + try { + const persistedInputsValue = core.getState(persistedInputsKey); + if (persistedInputsValue) { + const persistedInputs = JSON.parse(persistedInputsValue); + + for (const [name, value] of persistedInputs) { + process.env[name] = value; + } } + } catch (err) { + throw new Error(`Unable to restore inputs: ${getErrorMessage(err)}`); } }; diff --git a/src/codeql.ts b/src/codeql.ts index 65e73d9451..2dfc659962 100644 --- a/src/codeql.ts +++ b/src/codeql.ts @@ -951,7 +951,13 @@ async function getCodeQLForCmd( }, }, ).exec(); - return JSON.parse(extractorPath) as string; + try { + return JSON.parse(extractorPath) as string; + } catch (err) { + throw new Error( + `Failed to parse extractor path for '${language}' from CLI: ${getErrorMessage(err)}\nOutput was: ${extractorPath}`, + ); + } }, async resolveQueriesStartingPacks(queries: string[]): Promise { const codeqlArgs = [ diff --git a/src/sarif/index.ts b/src/sarif/index.ts index 3cd537dafb..ecb603a273 100644 --- a/src/sarif/index.ts +++ b/src/sarif/index.ts @@ -1,6 +1,7 @@ import * as fs from "fs"; import { Logger } from "../logging"; +import { getErrorMessage } from "../util"; import * as sarif from "sarif"; @@ -48,7 +49,13 @@ export function getToolNames(sarifFile: Partial): string[] { * @returns The resulting JSON value, cast to a SARIF `Log`. */ export function readSarifFile(sarifFilePath: string): Partial { - return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as sarif.Log; + try { + return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as sarif.Log; + } catch (err) { + throw new Error( + `Parsing SARIF file at '${sarifFilePath}' failed: ${getErrorMessage(err)}`, + ); + } } // Takes a list of paths to sarif files and combines them together, diff --git a/src/tracer-config.ts b/src/tracer-config.ts index d786d46515..fb3a0a05be 100644 --- a/src/tracer-config.ts +++ b/src/tracer-config.ts @@ -4,7 +4,7 @@ import * as path from "path"; import { type CodeQL } from "./codeql"; import { type Config } from "./config-utils"; import { Logger } from "./logging"; -import { asyncSome, BuildMode } from "./util"; +import { asyncSome, BuildMode, getErrorMessage } from "./util"; export type TracerConfig = { env: { [key: string]: string }; @@ -79,18 +79,20 @@ export async function endTracingForCluster( async function getTracerConfigForCluster( config: Config, ): Promise { - const tracingEnvVariables = JSON.parse( - fs.readFileSync( - path.resolve( - config.dbLocation, - "temp/tracingEnvironment/start-tracing.json", - ), - "utf8", - ), + const filePath = path.resolve( + config.dbLocation, + "temp/tracingEnvironment/start-tracing.json", ); - return { - env: tracingEnvVariables, - }; + try { + const tracingEnvVariables = JSON.parse(fs.readFileSync(filePath, "utf8")); + return { + env: tracingEnvVariables, + }; + } catch (err) { + throw new Error( + `Failed to parse tracing environment from '${filePath}': ${getErrorMessage(err)}`, + ); + } } export async function getCombinedTracerConfig( diff --git a/src/util.ts b/src/util.ts index 456cd7c3d2..be67a111b2 100644 --- a/src/util.ts +++ b/src/util.ts @@ -894,7 +894,13 @@ export function parseMatrixInput( if (matrixInput === undefined || matrixInput === "null") { return undefined; } - return JSON.parse(matrixInput) as { [key: string]: string }; + try { + return JSON.parse(matrixInput) as { [key: string]: string }; + } catch (err) { + throw new Error( + `Failed to parse matrix input '${matrixInput}': ${getErrorMessage(err)}`, + ); + } } export function wrapError(error: unknown): Error { @@ -1037,7 +1043,11 @@ export enum BuildMode { } export function cloneObject(obj: T): T { - return JSON.parse(JSON.stringify(obj)) as T; + try { + return JSON.parse(JSON.stringify(obj)) as T; + } catch (err) { + throw new Error(`Cloning object failed: ${getErrorMessage(err)}`); + } } export async function cleanUpPath(file: string, name: string, logger: Logger) {