Skip to content

Commit

Permalink
Handle JSON.parse exception gracefully.
Browse files Browse the repository at this point in the history
(cherry picked from commit ee87bdd)
  • Loading branch information
ssassi authored and Robbie Ostermann committed Jul 11, 2022
1 parent 0e67f6b commit e1d2691
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/features/sqlFluffLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,33 @@ export class SQLFluffLinterProvider implements Linter {
public process(lines: string[]): Diagnostic[] {
const diagnostics: Diagnostic[] = [];
lines.forEach((line) => {
const filePaths: Array<FilePath> = JSON.parse(line);
let filePaths: Array<FilePath>;
try {
filePaths = JSON.parse(line);
} catch (e) {
// JSON.parse may fail if sqlfluff compilation prints non-JSON formatted messages
console.warn(e);
}

filePaths.forEach((filePath: FilePath) => {
filePath.violations.forEach((violation: Violation) => {
const diagnostic = new Diagnostic(
new Range(
violation.line_no - 1,
violation.line_pos,
violation.line_no - 1,
violation.line_pos
),
violation.description,
DiagnosticSeverity.Error,
);
diagnostic.code = violation.code;
diagnostic.source = "sqlfluff";
diagnostics.push(diagnostic);
if (filePaths) {
filePaths.forEach((filePath: FilePath) => {
filePath.violations.forEach((violation: Violation) => {
const diagnostic = new Diagnostic(
new Range(
violation.line_no - 1,
violation.line_pos,
violation.line_no - 1,
violation.line_pos
),
violation.description,
DiagnosticSeverity.Error,
);
diagnostic.code = violation.code;
diagnostic.source = "sqlfluff";
diagnostics.push(diagnostic);
});
});
});

}
});

return diagnostics;
Expand Down

0 comments on commit e1d2691

Please sign in to comment.