Skip to content

Commit 08fae3c

Browse files
committed
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.
1 parent ffd96b3 commit 08fae3c

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

lib/upload-lib.js

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/upload-lib.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,20 @@ function getSarifFilePaths(sarifPath: string) {
176176
// Counts the number of results in the given SARIF file
177177
export function countResultsInSarif(sarif: string): number {
178178
let numResults = 0;
179-
for (const run of JSON.parse(sarif).runs) {
179+
let parsedSarif;
180+
try {
181+
parsedSarif = JSON.parse(sarif);
182+
} catch (e) {
183+
throw new Error(`Invalid SARIF. JSON syntax error: ${e.message}`);
184+
}
185+
if (!Array.isArray(parsedSarif.runs)) {
186+
throw new Error("Invalid SARIF. Missing 'runs' array.");
187+
}
188+
189+
for (const run of parsedSarif.runs) {
190+
if (!Array.isArray(run.results)) {
191+
throw new Error("Invalid SARIF. Missing 'results' array in run.");
192+
}
180193
numResults += run.results.length;
181194
}
182195
return numResults;

0 commit comments

Comments
 (0)