Skip to content

Commit

Permalink
output a better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Brignull committed May 22, 2020
1 parent ddee374 commit ae30190
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
18 changes: 13 additions & 5 deletions 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.

6 changes: 2 additions & 4 deletions lib/upload-lib.test.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.test.js.map

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

6 changes: 2 additions & 4 deletions src/upload-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import * as uploadLib from './upload-lib';

test('validateSarifFileSchema - valid', t => {
const inputFile = __dirname + '/../src/testdata/valid-sarif.sarif';
const errors = uploadLib.validateSarifFileSchema(inputFile);
t.deepEqual(errors, []);
t.true(uploadLib.validateSarifFileSchema(inputFile));
});

test('validateSarifFileSchema - invalid', t => {
const inputFile = __dirname + '/../src/testdata/invalid-sarif.sarif';
const errors = uploadLib.validateSarifFileSchema(inputFile);
t.notDeepEqual(errors, []);
t.false(uploadLib.validateSarifFileSchema(inputFile));
});
22 changes: 16 additions & 6 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,27 @@ export function countResultsInSarif(sarif: string): number {
// Validates that the given file path refers to a valid SARIF file.
// Returns a non-empty list of error message if the file is invalid,
// otherwise returns the empty list if the file is valid.
export function validateSarifFileSchema(sarifFilePath: string): string[] {
export function validateSarifFileSchema(sarifFilePath: string): boolean {
const sarif = JSON.parse(fs.readFileSync(sarifFilePath, 'utf8'));
const schema = JSON.parse(fs.readFileSync(__dirname + '/../src/sarif_v2.1.0_schema.json', 'utf8'));

const result = new jsonschema.Validator().validate(sarif, schema);
if (result.valid) {
return [];
return true;
} else {
return result.errors.map(e => e.message);
// Set the failure 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 errorMessages = result.errors.map(e => "- " + e.stack);
core.setFailed("Unable to upload \"" + sarifFilePath + "\" as it is not valid SARIF:\n" + errorMessages.join("\n"));

// Also output the more verbose error messages in groups as these may be very large.
for (const error of result.errors) {
core.startGroup("Error details: " + error.stack);
core.info(JSON.stringify(error, null, 2));
core.endGroup();
}

return false;
}
}

Expand All @@ -156,9 +168,7 @@ async function uploadFiles(sarifFiles: string[]): Promise<boolean> {

// Validate that the files we were asked to upload are all valid SARIF files
for (const file of sarifFiles) {
const errors = validateSarifFileSchema(file);
if (errors.length > 0) {
core.setFailed("Unable to upload \"" + file + "\" as it is not valid SARIF:\n" + errors.join("\n"));
if (!validateSarifFileSchema(file)) {
return false;
}
}
Expand Down

0 comments on commit ae30190

Please sign in to comment.