Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
monosans committed Oct 13, 2022
1 parent 78cc95b commit b5a1380
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ const versionedOptions = [
["preserve-blank-lines", "preserveBlankLines", "1.3.0"],
["preserve-leading-space", "preserveLeadingSpace", "1.2.0"],
];
const errorRegex = /Error.*/;
const goodStderrRegex = /Linting\s\d+\/\d+\sfiles/;

export function getErrorMsg(stderr: string): string | undefined {
if (stderr.includes("No module named")) {
export function getErrorMsg(stderr: string): string | null {
if (stderr.endsWith("No module named djlint")) {
return "djLint is not installed for the current active Python interpreter.";
}
for (const [cliOption, vscodeOption, minVersion] of versionedOptions) {
if (stderr.includes(`No such option: --${cliOption}`)) {
if (stderr.includes(`Error: No such option: --${cliOption}`)) {
return `Your version of djLint does not support the ${vscodeOption} option. Disable it in the settings or install djLint>=${minVersion}.`;
}
}
return stderr.match(errorRegex)?.toString();

// Workaround for djLint<1.18
if (goodStderrRegex.test(stderr)) {
return null;
}

return stderr;
}
1 change: 1 addition & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function runDjlint(
stderr += data;
});
child.on("close", () => {
stderr = stderr.trim();
const errMsg = getErrorMsg(stderr);
if (errMsg) {
void vscode.window.showErrorMessage(errMsg);
Expand Down

0 comments on commit b5a1380

Please sign in to comment.