diff options
author | Nayeem Rahman <muhammed.9939@gmail.com> | 2019-12-21 00:06:07 +0000 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-12-20 19:06:07 -0500 |
commit | b7b0668c782dd4bc92237d91116f033e57536238 (patch) | |
tree | 74d9f68e17d35030254435c7f1c03e9227b27c4d /cli/js | |
parent | e8d82a6348d4cf9fc6a023fe16bf75df7fea61b0 (diff) |
feat: Return null on error in Deno.dir() (#3531)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/lib.deno_runtime.d.ts | 4 | ||||
-rw-r--r-- | cli/js/os.ts | 14 | ||||
-rw-r--r-- | cli/js/os_test.ts | 7 |
3 files changed, 16 insertions, 9 deletions
diff --git a/cli/js/lib.deno_runtime.d.ts b/cli/js/lib.deno_runtime.d.ts index a93ce466f..e667d497d 100644 --- a/cli/js/lib.deno_runtime.d.ts +++ b/cli/js/lib.deno_runtime.d.ts @@ -74,6 +74,8 @@ declare namespace Deno { /** * Returns the user and platform specific directories. * Requires the `--allow-env` flag. + * Returns null if there is no applicable directory or if any other error + * occurs. * * Argument values: "home", "cache", "config", "data", "data_local", "audio", * "desktop", "document", "download", "font", "picture", "public", "template", @@ -170,7 +172,7 @@ declare namespace Deno { * | macOS | `$HOME`/Movies | /Users/Alice/Movies | * | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos | */ - export function dir(kind: DirKind): string; + export function dir(kind: DirKind): string | null; /** * Returns the path to the current deno executable. diff --git a/cli/js/os.ts b/cli/js/os.ts index baa62c220..8e91be4af 100644 --- a/cli/js/os.ts +++ b/cli/js/os.ts @@ -2,6 +2,7 @@ import { core } from "./core.ts"; import * as dispatch from "./dispatch.ts"; import { sendSync } from "./dispatch_json.ts"; +import { ErrorKind } from "./errors.ts"; import { assert } from "./util.ts"; import * as util from "./util.ts"; import { window } from "./window.ts"; @@ -147,6 +148,8 @@ type DirKind = /** * Returns the user and platform specific directories. * Requires the `--allow-env` flag. + * Returns null if there is no applicable directory or if any other error + * occurs. * * Argument values: "home", "cache", "config", "data", "data_local", "audio", * "desktop", "document", "download", "font", "picture", "public", "template", @@ -243,8 +246,15 @@ type DirKind = * | macOS | `$HOME`/Movies | /Users/Alice/Movies | * | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos | */ -export function dir(kind: DirKind): string { - return sendSync(dispatch.OP_GET_DIR, { kind }); +export function dir(kind: DirKind): string | null { + try { + return sendSync(dispatch.OP_GET_DIR, { kind }); + } catch (error) { + if (error.kind == ErrorKind.PermissionDenied) { + throw error; + } + return null; + } } /** diff --git a/cli/js/os_test.ts b/cli/js/os_test.ts index e77678ec1..55a62792d 100644 --- a/cli/js/os_test.ts +++ b/cli/js/os_test.ts @@ -242,12 +242,7 @@ testPerm({ env: true }, function getDir(): void { if (r.shouldHaveValue) { assertNotEquals(Deno.dir(s.kind), ""); } else { - // if not support your platform. it should throw an error - assertThrows( - () => Deno.dir(s.kind), - Deno.DenoError, - `Could not get user ${s.kind} directory.` - ); + assertEquals(Deno.dir(s.kind), null); } } } |