fix permission errors are swallowed by fs.exists (#3493)

This commit is contained in:
Axetroy 2019-12-13 22:47:09 +08:00 committed by Ry Dahl
parent df7d8288d9
commit 8cf8a29d35
4 changed files with 132 additions and 6 deletions

View file

@ -1,12 +1,20 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
const { lstat, lstatSync, DenoError, ErrorKind } = Deno;
/**
* Test whether or not the given path exists by checking with the file system
*/
export async function exists(filePath: string): Promise<boolean> {
return Deno.lstat(filePath)
return lstat(filePath)
.then((): boolean => true)
.catch((): boolean => false);
.catch((err: Error): boolean => {
if (err instanceof DenoError) {
if (err.kind === ErrorKind.NotFound) {
return false;
}
}
throw err;
});
}
/**
@ -14,9 +22,14 @@ export async function exists(filePath: string): Promise<boolean> {
*/
export function existsSync(filePath: string): boolean {
try {
Deno.lstatSync(filePath);
lstatSync(filePath);
return true;
} catch {
return false;
} catch (err) {
if (err instanceof DenoError) {
if (err.kind === ErrorKind.NotFound) {
return false;
}
}
throw err;
}
}