Skip to content

Commit c504d89

Browse files
committed
modified to display better error messages
1 parent 8690125 commit c504d89

6 files changed

Lines changed: 54 additions & 15 deletions

File tree

src/client/formatters/baseFormatter.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,27 @@ export abstract class BaseFormatter {
3737
}
3838
return getTextEditsFromPatch(document.getText(), data[1]);
3939
}).catch(error => {
40-
this.outputChannel.appendLine(error);
41-
throw new Error(`There was an error in formatting the document. View the Python output window for details.`);
40+
this.handleError(this.Id, command, error);
4241
});
4342
}
43+
44+
protected handleError(expectedFileName: string, fileName: string, error: Error) {
45+
let customError = "Formatting with ${this.Id} failed. Please install the formatter or turn it off.\n";
46+
47+
if (typeof (error) === "object" && error !== null && ((<any>error).code === "ENOENT" || (<any>error).code === 127)) {
48+
// Check if we have some custom arguments such as "pylint --load-plugins pylint_django"
49+
// Such settings are no longer supported
50+
let stuffAfterFileName = fileName.substring(fileName.toUpperCase().lastIndexOf(expectedFileName) + expectedFileName.length);
51+
52+
// Ok if we have a space after the file name, this means we have some arguments defined and this isn't supported
53+
if (stuffAfterFileName.trim().indexOf(" ")) {
54+
customError = `Formatting failed, custom arguments in the 'python.formatting.${this.Id}Path' is not supported.\n` +
55+
`Custom arguments to the formatter can be defined in 'python.formatter.${this.Id}Args' setting of settings.json.\n` +
56+
"For further details, please see https://github.com/DonJayamanne/pythonVSCode/wiki/Troubleshooting-Linting#2-linting-with-xxx-failed-";
57+
}
58+
}
59+
60+
this.outputChannel.appendLine(`${customError}\n${error}`);
61+
throw new Error(`There was an error in formatting the document. View the Python output window for details.`);
62+
}
4463
}

src/client/linters/baseLinter.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export abstract class BaseLinter {
6464

6565
return new Promise<ILintMessage[]>((resolve, reject) => {
6666
execPythonFile(command, args, cwd, true).then(data => {
67-
outputChannel.clear();
67+
outputChannel.append("#".repeat(10) + "Linting Output - " + this.Id + "#".repeat(10));
6868
outputChannel.append(data);
6969
var outputLines = data.split(/\r?\n/g);
7070
var diagnostics: ILintMessage[] = [];
@@ -108,10 +108,29 @@ export abstract class BaseLinter {
108108

109109
resolve(diagnostics);
110110
}).catch(error => {
111-
outputChannel.appendLine(`Linting with ${linterId} failed. If not installed please turn if off in settings.\n ${error}`);
112-
window.showInformationMessage(`Linting with ${linterId} failed. If not installed please turn if off in settings. View Python output for details.`);
111+
this.handleError(this.Id, command, error);
113112
return [];
114113
});
115114
});
116115
}
116+
117+
protected handleError(expectedFileName: string, fileName: string, error: Error) {
118+
let customError = "Linting with ${this.Id} failed. Please install the linter or turn it off.\n";
119+
120+
if (typeof (error) === "object" && error !== null && ((<any>error).code === "ENOENT" || (<any>error).code === 127)) {
121+
// Check if we have some custom arguments such as "pylint --load-plugins pylint_django"
122+
// Such settings are no longer supported
123+
let stuffAfterFileName = fileName.substring(fileName.toUpperCase().lastIndexOf(expectedFileName) + expectedFileName.length);
124+
125+
// Ok if we have a space after the file name, this means we have some arguments defined and this isn't supported
126+
if (stuffAfterFileName.trim().indexOf(" ")) {
127+
customError = `Linting failed, custom arguments in the 'python.linting.${this.Id}Path' is not supported.\n` +
128+
`Custom arguments to the linters can be defined in 'python.linting.${this.Id}Args' setting of settings.json.\n` +
129+
"For further details, please see https://github.com/DonJayamanne/pythonVSCode/wiki/Troubleshooting-Linting#2-linting-with-xxx-failed-";
130+
}
131+
}
132+
133+
this.outputChannel.appendLine(`${customError}\n${error}`);
134+
window.showInformationMessage(`${customError}. View Python output for details.`);
135+
}
117136
}

src/client/linters/prospector.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ export class Linter extends baseLinter.BaseLinter {
4141
let prospectorArgs = Array.isArray(this.pythonSettings.linting.prospectorArgs) ? this.pythonSettings.linting.prospectorArgs : [];
4242
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
4343
execPythonFile(prospectorPath, prospectorArgs.concat(["--absolute-paths", "--output-format=json", filePath]), this.workspaceRootPath, false).then(data => {
44-
outputChannel.clear();
4544
let parsedData: IProspectorResponse;
4645
try {
4746
parsedData = JSON.parse(data);
4847
}
4948
catch (ex) {
49+
outputChannel.append("#".repeat(10) + "Linting Output - " + this.Id + "#".repeat(10));
5050
outputChannel.append(data);
5151
return resolve([]);
5252
}
@@ -77,8 +77,8 @@ export class Linter extends baseLinter.BaseLinter {
7777

7878
resolve(diagnostics);
7979
}).catch(error => {
80-
outputChannel.appendLine(`Linting with ${linterId} failed.If not installed please turn if off in settings.\n ${error} `);
81-
window.showInformationMessage(`Linting with ${linterId} failed.If not installed please turn if off in settings.View Python output for details.`);
80+
this.handleError(this.Id, prospectorPath, error);
81+
return [];
8282
});
8383
});
8484
}

src/client/linters/pydocstyle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Linter extends baseLinter.BaseLinter {
4141
return new Promise<ILintMessage[]>((resolve, reject) => {
4242
var fileDir = path.dirname(filePath);
4343
execPythonFile(commandLine, args, this.workspaceRootPath, true).then(data => {
44-
outputChannel.clear();
44+
outputChannel.append("#".repeat(10) + "Linting Output - " + this.Id + "#".repeat(10));
4545
outputChannel.append(data);
4646
var outputLines = data.split(/\r?\n/g);
4747
var diagnostics: ILintMessage[] = [];
@@ -91,8 +91,8 @@ export class Linter extends baseLinter.BaseLinter {
9191
});
9292
resolve(diagnostics);
9393
}, error => {
94-
outputChannel.appendLine(`Linting with ${linterId} failed. If not installed please turn if off in settings.\n ${error}`);
95-
window.showInformationMessage(`Linting with ${linterId} failed. If not installed please turn if off in settings. View Python output for details.`);
94+
this.handleError(this.Id, commandLine, error);
95+
return [];
9696
});
9797
});
9898
}

src/client/providers/jediProxy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,11 @@ function onConfigChanged() {
398398
getPathFromPythonCommand(["-c", "import sys;print(sys.prefix)"]),
399399
// Python specific site packages
400400
getPathFromPythonCommand(["-c", "from distutils.sysconfig import get_python_lib; print(get_python_lib())"]),
401-
// Python global site packages
401+
// Python global site packages, as a fallback in case user hasn't installed them in custom environment
402402
getPathFromPythonCommand(["-m", "side", "--user-site"])
403403
];
404404
Promise.all<string>(filePaths).then(paths => {
405-
// additionalAutoCopletePaths = paths.filter(p => p.length > 0);
405+
additionalAutoCopletePaths = paths.filter(p => p.length > 0);
406406
});
407407
}
408408

@@ -414,9 +414,9 @@ function getConfig() {
414414
}
415415
return path.join(vscode.workspace.rootPath, extraPath);
416416
});
417-
417+
let distinctExtraPaths = extraPaths.concat(additionalAutoCopletePaths).filter((value, index, self) => self.indexOf(value) === index);
418418
return {
419-
extraPaths: extraPaths.concat(additionalAutoCopletePaths),
419+
extraPaths: distinctExtraPaths,
420420
useSnippets: false,
421421
caseInsensitiveCompletion: true,
422422
showDescriptions: true,

src/client/providers/lintProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export class LintProvider extends vscode.Disposable {
9898
});
9999

100100
this.pendingLintings.set(documentUri.fsPath, cancelToken);
101+
this.outputChannel.clear();
101102
let promises = this.linters.map(linter => {
102103
if (!linter.isEnabled()) {
103104
return Promise.resolve([]);

0 commit comments

Comments
 (0)