Skip to content

Commit

Permalink
Display better error message on invalid sarif
Browse files Browse the repository at this point in the history
Specifically, some third party tools do not include a `results`
block for runs when there is an error. This change adds a more
explicit error message for this situation.
  • Loading branch information
Andrew Eisenberg authored and Andrew Eisenberg committed Mar 18, 2021
1 parent ffd96b3 commit 08fae3c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
15 changes: 14 additions & 1 deletion lib/upload-lib.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/upload-lib.js.map

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

15 changes: 14 additions & 1 deletion src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,20 @@ function getSarifFilePaths(sarifPath: string) {
// Counts the number of results in the given SARIF file
export function countResultsInSarif(sarif: string): number {
let numResults = 0;
for (const run of JSON.parse(sarif).runs) {
let parsedSarif;
try {
parsedSarif = JSON.parse(sarif);
} catch (e) {
throw new Error(`Invalid SARIF. JSON syntax error: ${e.message}`);
}
if (!Array.isArray(parsedSarif.runs)) {
throw new Error("Invalid SARIF. Missing 'runs' array.");
}

for (const run of parsedSarif.runs) {
if (!Array.isArray(run.results)) {
throw new Error("Invalid SARIF. Missing 'results' array in run.");
}
numResults += run.results.length;
}
return numResults;
Expand Down

0 comments on commit 08fae3c

Please sign in to comment.