From e1d269155889c0e291b137721bd9954564b022cf Mon Sep 17 00:00:00 2001 From: Sebastian Sassi Date: Tue, 21 Jun 2022 16:22:56 -0300 Subject: [PATCH] Handle JSON.parse exception gracefully. (cherry picked from commit ee87bdd7568100707ba0231cf7a3a90589242bfd) --- src/features/sqlFluffLinter.ts | 43 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/features/sqlFluffLinter.ts b/src/features/sqlFluffLinter.ts index 23dee43..2ee6047 100644 --- a/src/features/sqlFluffLinter.ts +++ b/src/features/sqlFluffLinter.ts @@ -16,26 +16,33 @@ export class SQLFluffLinterProvider implements Linter { public process(lines: string[]): Diagnostic[] { const diagnostics: Diagnostic[] = []; lines.forEach((line) => { - const filePaths: Array = JSON.parse(line); + let filePaths: Array; + 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;