Display better error message on invalid sarif

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.
This commit is contained in:
Andrew Eisenberg 2021-03-17 09:36:00 -07:00 committed by Andrew Eisenberg
parent ffd96b38fb
commit 08fae3caba
3 changed files with 29 additions and 3 deletions

View file

@ -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;