Rename InvalidRequestError to InvalidSarifUploadError

Nitty: make it a little clearer when this shows up in the logs what type of request we mean
This commit is contained in:
Henry Mercer 2024-02-28 19:25:30 +00:00
parent 7bde9061b4
commit bd56a05133
3 changed files with 23 additions and 23 deletions

22
lib/upload-lib.js generated
View file

@ -56,7 +56,7 @@ function combineSarifFiles(sarifFiles) {
combinedSarif.version = sarifObject.version;
}
else if (combinedSarif.version !== sarifObject.version) {
throw new InvalidRequestError(`Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}`);
throw new InvalidSarifUploadError(`Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}`);
}
combinedSarif.runs.push(...sarifObject.runs);
}
@ -162,7 +162,7 @@ async function uploadFromActions(sarifPath, checkoutPath, category, logger, { is
return await uploadFiles(getSarifFilePaths(sarifPath), (0, repository_1.parseRepositoryNwo)(util.getRequiredEnvParam("GITHUB_REPOSITORY")), await actionsUtil.getCommitOid(checkoutPath), await actionsUtil.getRef(), await api.getAnalysisKey(), category, util.getRequiredEnvParam("GITHUB_WORKFLOW"), actionsUtil.getWorkflowRunID(), actionsUtil.getWorkflowRunAttempt(), checkoutPath, actionsUtil.getRequiredInput("matrix"), logger);
}
catch (e) {
if (e instanceof InvalidRequestError && isThirdPartyUpload) {
if (e instanceof InvalidSarifUploadError && isThirdPartyUpload) {
throw new util_1.ConfigurationError(e.message);
}
throw e;
@ -171,13 +171,13 @@ async function uploadFromActions(sarifPath, checkoutPath, category, logger, { is
exports.uploadFromActions = uploadFromActions;
function getSarifFilePaths(sarifPath) {
if (!fs.existsSync(sarifPath)) {
throw new InvalidRequestError(`Path does not exist: ${sarifPath}`);
throw new InvalidSarifUploadError(`Path does not exist: ${sarifPath}`);
}
let sarifFiles;
if (fs.lstatSync(sarifPath).isDirectory()) {
sarifFiles = findSarifFilesInDir(sarifPath);
if (sarifFiles.length === 0) {
throw new InvalidRequestError(`No SARIF files found to upload in "${sarifPath}".`);
throw new InvalidSarifUploadError(`No SARIF files found to upload in "${sarifPath}".`);
}
}
else {
@ -190,11 +190,11 @@ function countResultsInSarif(sarif) {
let numResults = 0;
const parsedSarif = JSON.parse(sarif);
if (!Array.isArray(parsedSarif.runs)) {
throw new InvalidRequestError("Invalid SARIF. Missing 'runs' array.");
throw new InvalidSarifUploadError("Invalid SARIF. Missing 'runs' array.");
}
for (const run of parsedSarif.runs) {
if (!Array.isArray(run.results)) {
throw new InvalidRequestError("Invalid SARIF. Missing 'results' array in run.");
throw new InvalidSarifUploadError("Invalid SARIF. Missing 'results' array in run.");
}
numResults += run.results.length;
}
@ -208,7 +208,7 @@ function validateSarifFileSchema(sarifFilePath, logger) {
sarif = JSON.parse(fs.readFileSync(sarifFilePath, "utf8"));
}
catch (e) {
throw new InvalidRequestError(`Invalid SARIF. JSON syntax error: ${(0, util_1.wrapError)(e).message}`);
throw new InvalidSarifUploadError(`Invalid SARIF. JSON syntax error: ${(0, util_1.wrapError)(e).message}`);
}
const schema = require("../src/sarif-schema-2.1.0.json");
const result = new jsonschema.Validator().validate(sarif, schema);
@ -229,7 +229,7 @@ function validateSarifFileSchema(sarifFilePath, logger) {
// Set the main error message to the stacks of all the errors.
// This should be of a manageable size and may even give enough to fix the error.
const sarifErrors = errors.map((e) => `- ${e.stack}`);
throw new InvalidRequestError(`Unable to upload "${sarifFilePath}" as it is not valid SARIF:\n${sarifErrors.join("\n")}`);
throw new InvalidSarifUploadError(`Unable to upload "${sarifFilePath}" as it is not valid SARIF:\n${sarifErrors.join("\n")}`);
}
}
exports.validateSarifFileSchema = validateSarifFileSchema;
@ -366,7 +366,7 @@ async function waitForProcessing(repositoryNwo, sarifID, logger, options = {
const message = `Code Scanning could not process the submitted SARIF file:\n${response.data.errors}`;
throw shouldConsiderConfigurationError(response.data.errors)
? new util_1.ConfigurationError(message)
: new InvalidRequestError(message);
: new InvalidSarifUploadError(message);
}
else {
util.assertNever(status);
@ -429,7 +429,7 @@ function validateUniqueCategory(sarif) {
for (const [category, { id, tool }] of Object.entries(categories)) {
const sentinelEnvVar = `CODEQL_UPLOAD_SARIF_${category}`;
if (process.env[sentinelEnvVar]) {
throw new InvalidRequestError("Aborting upload: only one run of the codeql/analyze or codeql/upload-sarif actions is allowed per job per tool/category. " +
throw new InvalidSarifUploadError("Aborting upload: only one run of the codeql/analyze or codeql/upload-sarif actions is allowed per job per tool/category. " +
"The easiest fix is to specify a unique value for the `category` input. If .runs[].automationDetails.id is specified " +
"in the sarif file, that will take precedence over your configured `category`. " +
`Category: (${id ? id : "none"}) Tool: (${tool ? tool : "none"})`);
@ -453,7 +453,7 @@ function sanitize(str) {
/**
* An error that occurred due to an invalid SARIF upload request.
*/
class InvalidRequestError extends Error {
class InvalidSarifUploadError extends Error {
constructor(message) {
super(message);
}

File diff suppressed because one or more lines are too long

View file

@ -37,7 +37,7 @@ function combineSarifFiles(sarifFiles: string[]): SarifFile {
if (combinedSarif.version === null) {
combinedSarif.version = sarifObject.version;
} else if (combinedSarif.version !== sarifObject.version) {
throw new InvalidRequestError(
throw new InvalidSarifUploadError(
`Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}`,
);
}
@ -207,7 +207,7 @@ export async function uploadFromActions(
logger,
);
} catch (e) {
if (e instanceof InvalidRequestError && isThirdPartyUpload) {
if (e instanceof InvalidSarifUploadError && isThirdPartyUpload) {
throw new ConfigurationError(e.message);
}
throw e;
@ -216,14 +216,14 @@ export async function uploadFromActions(
function getSarifFilePaths(sarifPath: string) {
if (!fs.existsSync(sarifPath)) {
throw new InvalidRequestError(`Path does not exist: ${sarifPath}`);
throw new InvalidSarifUploadError(`Path does not exist: ${sarifPath}`);
}
let sarifFiles: string[];
if (fs.lstatSync(sarifPath).isDirectory()) {
sarifFiles = findSarifFilesInDir(sarifPath);
if (sarifFiles.length === 0) {
throw new InvalidRequestError(
throw new InvalidSarifUploadError(
`No SARIF files found to upload in "${sarifPath}".`,
);
}
@ -238,12 +238,12 @@ function countResultsInSarif(sarif: string): number {
let numResults = 0;
const parsedSarif = JSON.parse(sarif);
if (!Array.isArray(parsedSarif.runs)) {
throw new InvalidRequestError("Invalid SARIF. Missing 'runs' array.");
throw new InvalidSarifUploadError("Invalid SARIF. Missing 'runs' array.");
}
for (const run of parsedSarif.runs) {
if (!Array.isArray(run.results)) {
throw new InvalidRequestError(
throw new InvalidSarifUploadError(
"Invalid SARIF. Missing 'results' array in run.",
);
}
@ -259,7 +259,7 @@ export function validateSarifFileSchema(sarifFilePath: string, logger: Logger) {
try {
sarif = JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile;
} catch (e) {
throw new InvalidRequestError(
throw new InvalidSarifUploadError(
`Invalid SARIF. JSON syntax error: ${wrapError(e).message}`,
);
}
@ -292,7 +292,7 @@ export function validateSarifFileSchema(sarifFilePath: string, logger: Logger) {
// Set the main error message to the stacks of all the errors.
// This should be of a manageable size and may even give enough to fix the error.
const sarifErrors = errors.map((e) => `- ${e.stack}`);
throw new InvalidRequestError(
throw new InvalidSarifUploadError(
`Unable to upload "${sarifFilePath}" as it is not valid SARIF:\n${sarifErrors.join(
"\n",
)}`,
@ -511,7 +511,7 @@ export async function waitForProcessing(
const message = `Code Scanning could not process the submitted SARIF file:\n${response.data.errors}`;
throw shouldConsiderConfigurationError(response.data.errors as string[])
? new ConfigurationError(message)
: new InvalidRequestError(message);
: new InvalidSarifUploadError(message);
} else {
util.assertNever(status);
}
@ -588,7 +588,7 @@ export function validateUniqueCategory(sarif: SarifFile): void {
for (const [category, { id, tool }] of Object.entries(categories)) {
const sentinelEnvVar = `CODEQL_UPLOAD_SARIF_${category}`;
if (process.env[sentinelEnvVar]) {
throw new InvalidRequestError(
throw new InvalidSarifUploadError(
"Aborting upload: only one run of the codeql/analyze or codeql/upload-sarif actions is allowed per job per tool/category. " +
"The easiest fix is to specify a unique value for the `category` input. If .runs[].automationDetails.id is specified " +
"in the sarif file, that will take precedence over your configured `category`. " +
@ -615,7 +615,7 @@ function sanitize(str?: string) {
/**
* An error that occurred due to an invalid SARIF upload request.
*/
class InvalidRequestError extends Error {
class InvalidSarifUploadError extends Error {
constructor(message: string) {
super(message);
}