Skip to content

Commit 0dea2ac

Browse files
Add error handling for parser
1 parent 8917115 commit 0dea2ac

1 file changed

Lines changed: 45 additions & 29 deletions

File tree

  • extensions/ql-vscode/src

extensions/ql-vscode/src/cli.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import * as child_process from 'child_process';
33
import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import { parser } from 'stream-json';
6-
import { chain } from 'stream-chain';
6+
import { pick } from 'stream-json/filters/Pick';
7+
import { verifier } from 'stream-json/utils/Verifier';
78
import Assembler = require('stream-json/Assembler');
9+
import { chain } from 'stream-chain';
810
import * as sarif from 'sarif';
911
import { SemVer } from 'semver';
1012
import { Readable } from 'stream';
@@ -20,9 +22,6 @@ import { assertNever } from './pure/helpers-pure';
2022
import { QueryMetadata, SortDirection } from './pure/interface-types';
2123
import { Logger, ProgressReporter } from './logging';
2224
import { CompilationMessage } from './pure/messages';
23-
// import Chain = require('stream-chain');
24-
import { pick } from 'stream-json/filters/Pick';
25-
// import { filter } from 'stream-json/filters/Filter';
2625

2726

2827
/**
@@ -676,39 +675,56 @@ export class CodeQLCliServer implements Disposable {
676675

677676
async interpretBqrs(metadata: QueryMetadata, resultsPath: string, interpretedResultsPath: string, sourceInfo?: SourceInfo): Promise<sarif.Log> {
678677
await this.runInterpretCommand(SARIF_FORMAT, metadata, resultsPath, interpretedResultsPath, sourceInfo);
679-
const fakeSarifPath = '/Users/marcjaramillo/MLH/jdk11-taintedpath-large-ungrouped.sarif';
678+
680679
try {
680+
// Parse the SARIF file into token streams, filtering out only the results array.
681681
const p = parser();
682-
//TODO: see if there is a way to create a parser that will return the count of codeFlow
683-
// paths first and then parse the SARIF based on whether it exceeds a certain threshhold.
684-
// Also, see if it's possible to parse only the first 5 or so codeFlows right up front
685-
// rather than parsing everything and then getting rid of the paths we don't want.
686-
// Update 11-10-2021: with the new test file, everything works much better so this may not be necessary.
687682
const pipeline = chain([
688-
fs.createReadStream(fakeSarifPath),
683+
fs.createReadStream(interpretedResultsPath),
689684
p,
690-
pick({filter: 'runs.0.results'})
685+
pick({filter: 'runs.0.results'}),
686+
verifier()
691687
]);
692688

689+
// Creates JavaScript objects from the token stream
693690
const asm = Assembler.connectTo(pipeline);
694-
//TODO: figure out error handling for Assembler
695-
return await new Promise((resolve) => {
696-
asm.on('done', (asm) => {
697-
const dummyTool : sarif.Tool = {driver: {name: ''}};
698-
699-
const log : sarif.Log = {
700-
version: '2.1.0',
701-
runs: [
702-
{
703-
tool: dummyTool,
704-
results: asm.current
705-
}
706-
]
707-
};
708-
709-
console.log('Log completed');
710-
resolve(log);
691+
692+
// Returns a constructed Log object with the results or an empty array if no results were found.
693+
// If the parser fails for any reason, it will reject the promise.
694+
return await new Promise((resolve, reject) => {
695+
pipeline.on('error', (error) => {
696+
reject(error);
711697
});
698+
699+
asm.on('done', (asm) => {
700+
const dummyTool : sarif.Tool = {driver: {name: ''}};
701+
if (asm.current) {
702+
const log : sarif.Log = {
703+
version: '2.1.0',
704+
runs: [
705+
{
706+
tool: dummyTool,
707+
results: asm.current
708+
}
709+
]
710+
};
711+
712+
resolve(log);
713+
} else {
714+
const log : sarif.Log = {
715+
version: '2.1.0',
716+
runs: [
717+
{
718+
tool: dummyTool,
719+
results: []
720+
}
721+
]
722+
};
723+
724+
resolve(log);
725+
}
726+
727+
});
712728
});
713729
} catch (err) {
714730
throw new Error(`Parsing output of interpretation failed: ${err.stderr || err}`);

0 commit comments

Comments
 (0)