From 17378616a2d301a97e631c5a33156e421df44324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 21 Feb 2020 10:36:13 -0500 Subject: [PATCH] refactor: remove unneeded ErrorKinds (denoland/deno#3936) --- encoding/yaml/error.ts | 6 ++---- fs/copy.ts | 4 ++-- fs/empty_dir.ts | 14 +++----------- fs/ensure_dir.ts | 6 +++--- fs/ensure_file.ts | 6 +++--- fs/ensure_link_test.ts | 6 ++---- fs/exists.ts | 14 +++++--------- fs/expand_glob.ts | 6 ++---- fs/walk_test.ts | 9 +++------ http/file_server.ts | 4 ++-- node/module.ts | 2 +- node/process_test.ts | 4 +--- path/glob.ts | 7 +------ testing/runner.ts | 4 ++-- 14 files changed, 32 insertions(+), 60 deletions(-) diff --git a/encoding/yaml/error.ts b/encoding/yaml/error.ts index 62be8b477d21..7f305ccf2c5d 100644 --- a/encoding/yaml/error.ts +++ b/encoding/yaml/error.ts @@ -5,14 +5,12 @@ import { Mark } from "./mark.ts"; -const { DenoError, ErrorKind } = Deno; - -export class YAMLError extends DenoError { +export class YAMLError extends Error { constructor( message = "(unknown reason)", protected mark: Mark | string = "" ) { - super(ErrorKind.Other, `${message} ${mark}`); + super(`${message} ${mark}`); this.name = this.constructor.name; } diff --git a/fs/copy.ts b/fs/copy.ts index 86fac78dff74..ec51784c6646 100644 --- a/fs/copy.ts +++ b/fs/copy.ts @@ -29,7 +29,7 @@ async function ensureValidCopy( try { destStat = await Deno.lstat(dest); } catch (err) { - if (err instanceof Deno.DenoError && err.kind == Deno.ErrorKind.NotFound) { + if (err instanceof Deno.Err.NotFound) { return; } throw err; @@ -57,7 +57,7 @@ function ensureValidCopySync( try { destStat = Deno.lstatSync(dest); } catch (err) { - if (err instanceof Deno.DenoError && err.kind == Deno.ErrorKind.NotFound) { + if (err instanceof Deno.Err.NotFound) { return; } throw err; diff --git a/fs/empty_dir.ts b/fs/empty_dir.ts index e3d08ef70a35..0ac5e6420cfd 100644 --- a/fs/empty_dir.ts +++ b/fs/empty_dir.ts @@ -1,14 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { join } from "../path/mod.ts"; -const { - readDir, - readDirSync, - mkdir, - mkdirSync, - remove, - removeSync, - ErrorKind -} = Deno; +const { readDir, readDirSync, mkdir, mkdirSync, remove, removeSync } = Deno; /** * Ensures that a directory is empty. * Deletes directory contents if the directory is not empty. @@ -28,7 +20,7 @@ export async function emptyDir(dir: string): Promise { } } } catch (err) { - if ((err as Deno.DenoError).kind !== ErrorKind.NotFound) { + if (!(err instanceof Deno.Err.NotFound)) { throw err; } @@ -57,7 +49,7 @@ export function emptyDirSync(dir: string): void { } } } catch (err) { - if ((err as Deno.DenoError).kind !== ErrorKind.NotFound) { + if (!(err instanceof Deno.Err.NotFound)) { throw err; } // if not exist. then create it diff --git a/fs/ensure_dir.ts b/fs/ensure_dir.ts index d4b30dd2d911..b6cc3150a356 100644 --- a/fs/ensure_dir.ts +++ b/fs/ensure_dir.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { getFileInfoType } from "./utils.ts"; -const { lstat, lstatSync, mkdir, mkdirSync, ErrorKind } = Deno; +const { lstat, lstatSync, mkdir, mkdirSync } = Deno; /** * Ensures that the directory exists. @@ -16,7 +16,7 @@ export async function ensureDir(dir: string): Promise { ); } } catch (err) { - if (err instanceof Deno.DenoError && err.kind === ErrorKind.NotFound) { + if (err instanceof Deno.Err.NotFound) { // if dir not exists. then create it. await mkdir(dir, { recursive: true }); return; @@ -39,7 +39,7 @@ export function ensureDirSync(dir: string): void { ); } } catch (err) { - if (err instanceof Deno.DenoError && err.kind == ErrorKind.NotFound) { + if (err instanceof Deno.Err.NotFound) { // if dir not exists. then create it. mkdirSync(dir, { recursive: true }); return; diff --git a/fs/ensure_file.ts b/fs/ensure_file.ts index 06c65b5f77e3..be824b7bac12 100644 --- a/fs/ensure_file.ts +++ b/fs/ensure_file.ts @@ -2,7 +2,7 @@ import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; import { getFileInfoType } from "./utils.ts"; -const { lstat, lstatSync, writeFile, writeFileSync, ErrorKind } = Deno; +const { lstat, lstatSync, writeFile, writeFileSync } = Deno; /** * Ensures that the file exists. @@ -23,7 +23,7 @@ export async function ensureFile(filePath: string): Promise { } } catch (err) { // if file not exists - if (err instanceof Deno.DenoError && err.kind === ErrorKind.NotFound) { + if (err instanceof Deno.Err.NotFound) { // ensure dir exists await ensureDir(path.dirname(filePath)); // create file @@ -54,7 +54,7 @@ export function ensureFileSync(filePath: string): void { } } catch (err) { // if file not exists - if (err instanceof Deno.DenoError && err.kind === ErrorKind.NotFound) { + if (err instanceof Deno.Err.NotFound) { // ensure dir exists ensureDirSync(path.dirname(filePath)); // create file diff --git a/fs/ensure_link_test.ts b/fs/ensure_link_test.ts index 6e9804152aa5..7549814a25d9 100644 --- a/fs/ensure_link_test.ts +++ b/fs/ensure_link_test.ts @@ -143,8 +143,7 @@ Deno.test(async function ensureLinkDirectoryIfItExist(): Promise { await assertThrowsAsync( async (): Promise => { await ensureLink(testDir, linkDir); - }, - Deno.DenoError + } // "Operation not permitted (os error 1)" // throw an local matching test // "Access is denied. (os error 5)" // throw in CI ); @@ -163,8 +162,7 @@ Deno.test(function ensureLinkSyncDirectoryIfItExist(): void { assertThrows( (): void => { ensureLinkSync(testDir, linkDir); - }, - Deno.DenoError + } // "Operation not permitted (os error 1)" // throw an local matching test // "Access is denied. (os error 5)" // throw in CI ); diff --git a/fs/exists.ts b/fs/exists.ts index 4584dbff96f7..2cd4151739d5 100644 --- a/fs/exists.ts +++ b/fs/exists.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { lstat, lstatSync, DenoError, ErrorKind } = Deno; +const { lstat, lstatSync } = Deno; /** * Test whether or not the given path exists by checking with the file system */ @@ -7,10 +7,8 @@ export async function exists(filePath: string): Promise { return lstat(filePath) .then((): boolean => true) .catch((err: Error): boolean => { - if (err instanceof DenoError) { - if (err.kind === ErrorKind.NotFound) { - return false; - } + if (err instanceof Deno.Err.NotFound) { + return false; } throw err; @@ -25,10 +23,8 @@ export function existsSync(filePath: string): boolean { lstatSync(filePath); return true; } catch (err) { - if (err instanceof DenoError) { - if (err.kind === ErrorKind.NotFound) { - return false; - } + if (err instanceof Deno.Err.NotFound) { + return false; } throw err; } diff --git a/fs/expand_glob.ts b/fs/expand_glob.ts index aabf4a8a1b2f..61fca96028c9 100644 --- a/fs/expand_glob.ts +++ b/fs/expand_glob.ts @@ -10,9 +10,7 @@ import { } from "../path/mod.ts"; import { WalkInfo, walk, walkSync } from "./walk.ts"; import { assert } from "../testing/asserts.ts"; -const { ErrorKind, cwd, stat, statSync } = Deno; -type ErrorKind = Deno.ErrorKind; -type DenoError = Deno.DenoError; +const { cwd, stat, statSync } = Deno; type FileInfo = Deno.FileInfo; export interface ExpandGlobOptions extends GlobOptions { @@ -45,7 +43,7 @@ function split(path: string): SplitPath { } function throwUnlessNotFound(error: Error): void { - if ((error as DenoError).kind != ErrorKind.NotFound) { + if (!(error instanceof Deno.Err.NotFound)) { throw error; } } diff --git a/fs/walk_test.ts b/fs/walk_test.ts index 4984546f2142..f99f82eb5162 100644 --- a/fs/walk_test.ts +++ b/fs/walk_test.ts @@ -1,7 +1,5 @@ -const { DenoError, ErrorKind, cwd, chdir, makeTempDir, mkdir, open } = Deno; +const { cwd, chdir, makeTempDir, mkdir, open } = Deno; const { remove } = Deno; -type ErrorKind = Deno.ErrorKind; -type DenoError = Deno.DenoError; import { walk, walkSync, WalkOptions, WalkInfo } from "./walk.ts"; import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts"; @@ -235,10 +233,9 @@ testWalk( testWalk( async (_d: string): Promise => {}, async function nonexistentRoot(): Promise { - const error = (await assertThrowsAsync(async () => { + await assertThrowsAsync(async () => { await walkArray("nonexistent"); - }, DenoError)) as DenoError; - assertEquals(error.kind, ErrorKind.NotFound); + }, Deno.Err.NotFound); } ); diff --git a/http/file_server.ts b/http/file_server.ts index aa0ff49daa08..acc3a20cd20a 100755 --- a/http/file_server.ts +++ b/http/file_server.ts @@ -6,7 +6,7 @@ // TODO Add tests like these: // https://github.com/indexzero/http-server/blob/master/test/http-server-test.js -const { ErrorKind, DenoError, args, stat, readDir, open, exit } = Deno; +const { args, stat, readDir, open, exit } = Deno; import { posix } from "../path/mod.ts"; import { listenAndServe, @@ -163,7 +163,7 @@ async function serveDir( } async function serveFallback(req: ServerRequest, e: Error): Promise { - if (e instanceof DenoError && e.kind === ErrorKind.NotFound) { + if (e instanceof Deno.Err.NotFound) { return { status: 404, body: encoder.encode("Not found") diff --git a/node/module.ts b/node/module.ts index 547c76bab179..aecf03ede6f4 100644 --- a/node/module.ts +++ b/node/module.ts @@ -57,7 +57,7 @@ function stat(filename: string): StatResult { if (statCache !== null) statCache.set(filename, result); return result; } catch (e) { - if (e.kind === Deno.ErrorKind.PermissionDenied) { + if (e instanceof Deno.Err.PermissionDenied) { throw new Error("CJS loader requires --allow-read."); } return -1; diff --git a/node/process_test.ts b/node/process_test.ts index 51dbcd630b22..545b6ce23f75 100644 --- a/node/process_test.ts +++ b/node/process_test.ts @@ -24,13 +24,11 @@ test({ () => { process.chdir("non-existent-directory-name"); }, - Deno.DenoError, + Deno.Err.NotFound, "file" // On every OS Deno returns: "No such file" except for Windows, where it's: // "The system cannot find the file specified. (os error 2)" so "file" is // the only common string here. - // TODO(rsp): Crazy idea: 404 for things like this? - // It would be nice to have error codes like 404 or 403 in addition to strings. ); } }); diff --git a/path/glob.ts b/path/glob.ts index a079f448f05e..8eb106b251da 100644 --- a/path/glob.ts +++ b/path/glob.ts @@ -3,8 +3,6 @@ import { globrex } from "./globrex.ts"; import { join, normalize } from "./mod.ts"; import { assert } from "../testing/asserts.ts"; -const { DenoError, ErrorKind } = Deno; - export interface GlobOptions { extended?: boolean; globstar?: boolean; @@ -91,10 +89,7 @@ export function normalizeGlob( { globstar = false }: GlobOptions = {} ): string { if (!!glob.match(/\0/g)) { - throw new DenoError( - ErrorKind.InvalidPath, - `Glob contains invalid characters: "${glob}"` - ); + throw new Error(`Glob contains invalid characters: "${glob}"`); } if (!globstar) { return normalize(glob); diff --git a/testing/runner.ts b/testing/runner.ts index 8d6501e0295b..772e027241cf 100755 --- a/testing/runner.ts +++ b/testing/runner.ts @@ -3,7 +3,7 @@ import { parse } from "../flags/mod.ts"; import { ExpandGlobOptions, expandGlob } from "../fs/mod.ts"; import { isWindows, join } from "../path/mod.ts"; -const { DenoError, ErrorKind, args, cwd, exit } = Deno; +const { args, cwd, exit } = Deno; const DIR_GLOBS = [join("**", "?(*_)test.{js,ts}")]; @@ -182,7 +182,7 @@ export async function runTestModules({ if (moduleCount == 0) { const noneFoundMessage = "No matching test modules found."; if (!allowNone) { - throw new DenoError(ErrorKind.NotFound, noneFoundMessage); + throw new Deno.Err.NotFound(noneFoundMessage); } else if (!disableLog) { console.log(noneFoundMessage); }