Skip to content

Commit 1e4a476

Browse files
authored
Show detailed error messages when a server-side URI is invalid (#1772)
1 parent 3b426fe commit 1e4a476

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,21 @@ export function isfsDocumentName(uri: vscode.Uri, csp?: boolean, pkg = false): s
247247
* and `/%Library/CHUIScreen.CLS` (extension has wrong case). This is needed to
248248
* prevent the user from opening multiple copies of the same document. This
249249
* function does not return a value; it throws a `vscode.FileSystemError.FileNotFound`
250-
* error when `uri`'s path is not in "canonical form".
250+
* error, or a more descriptive custom error when `descriptiveError` is `true`,
251+
* when `uri`'s path is not in "canonical form".
251252
*/
252-
function validateUriIsCanonical(uri: vscode.Uri): void {
253+
function validateUriIsCanonical(uri: vscode.Uri, descriptiveError = false): void {
253254
const numDots = uri.path.split(".").length - 1;
254255
const lastFour = uri.path.slice(-4);
255-
if (
256-
!isfsConfig(uri).csp &&
257-
[".cls", ".mac", ".int", ".inc"].includes(lastFour.toLowerCase()) &&
258-
// extension has wrong case
259-
(![".cls", ".mac", ".int", ".inc"].includes(lastFour) ||
260-
// short alias for %Library class
261-
(uri.path.startsWith("/%") && lastFour == ".cls" && numDots == 1 && uri.path.split("/").length == 2) ||
262-
// dotted packages
263-
(numDots > 1 && !(numDots == 2 && /\.G?\d\.int$/.test(uri.path))))
264-
) {
265-
throw vscode.FileSystemError.FileNotFound(uri);
256+
if (!isfsConfig(uri).csp && [".cls", ".mac", ".int", ".inc"].includes(lastFour.toLowerCase())) {
257+
const msg = ![".cls", ".mac", ".int", ".inc"].includes(lastFour)
258+
? "File extension must be in all lowercase"
259+
: uri.path.startsWith("/%") && lastFour == ".cls" && numDots == 1 && uri.path.split("/").length == 2
260+
? "Must use the full name of %Library classes"
261+
: numDots > 1 && !(numDots == 2 && /\.G?\d\.int$/.test(uri.path))
262+
? "Packages must use forward slashes as delimiters instead of dots"
263+
: undefined;
264+
if (msg) throw descriptiveError ? new vscode.FileSystemError(msg) : vscode.FileSystemError.FileNotFound(uri);
266265
}
267266
}
268267

@@ -524,7 +523,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
524523
const originalUri = vscode.Uri.parse(originalUriString);
525524
this._needsUpdate.delete(originalUriString);
526525
uri = redirectDotvscodeRoot(uri, new vscode.FileSystemError("Server does not have a /_vscode web application"));
527-
validateUriIsCanonical(uri);
526+
validateUriIsCanonical(uri, true);
528527
const csp = isCSP(uri);
529528
const fileName = isfsDocumentName(uri, csp);
530529
if (fileName.startsWith(".")) {
@@ -815,7 +814,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
815814
throw new vscode.FileSystemError("Cannot rename a file across workspace folders");
816815
}
817816
validateUriIsCanonical(oldUri);
818-
validateUriIsCanonical(newUri);
817+
validateUriIsCanonical(newUri, true);
819818
// Check if the destination exists
820819
let newFileStat: vscode.FileStat;
821820
try {

0 commit comments

Comments
 (0)