Skip to content

Commit

Permalink
refactor: remove unneeded ErrorKinds (denoland/deno#3936)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored and caspervonb committed Jan 31, 2021
1 parent 6647b29 commit 1737861
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 60 deletions.
6 changes: 2 additions & 4 deletions encoding/yaml/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

import { Mark } from "./mark.ts";

const { DenoError, ErrorKind } = Deno;

export class YAMLError extends DenoError<typeof ErrorKind.Other> {
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;
}

Expand Down
4 changes: 2 additions & 2 deletions fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 3 additions & 11 deletions fs/empty_dir.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -28,7 +20,7 @@ export async function emptyDir(dir: string): Promise<void> {
}
}
} catch (err) {
if ((err as Deno.DenoError<Deno.ErrorKind>).kind !== ErrorKind.NotFound) {
if (!(err instanceof Deno.Err.NotFound)) {
throw err;
}

Expand Down Expand Up @@ -57,7 +49,7 @@ export function emptyDirSync(dir: string): void {
}
}
} catch (err) {
if ((err as Deno.DenoError<Deno.ErrorKind>).kind !== ErrorKind.NotFound) {
if (!(err instanceof Deno.Err.NotFound)) {
throw err;
}
// if not exist. then create it
Expand Down
6 changes: 3 additions & 3 deletions fs/ensure_dir.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,7 +16,7 @@ export async function ensureDir(dir: string): Promise<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.
await mkdir(dir, { recursive: true });
return;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions fs/ensure_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,7 +23,7 @@ export async function ensureFile(filePath: string): Promise<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
await ensureDir(path.dirname(filePath));
// create file
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions fs/ensure_link_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ Deno.test(async function ensureLinkDirectoryIfItExist(): Promise<void> {
await assertThrowsAsync(
async (): Promise<void> => {
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
);
Expand All @@ -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
);
Expand Down
14 changes: 5 additions & 9 deletions fs/exists.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// 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
*/
export async function exists(filePath: string): Promise<boolean> {
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;
Expand All @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions fs/expand_glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ErrorKind>;
const { cwd, stat, statSync } = Deno;
type FileInfo = Deno.FileInfo;

export interface ExpandGlobOptions extends GlobOptions {
Expand Down Expand Up @@ -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;
}
}
Expand Down
9 changes: 3 additions & 6 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
@@ -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<ErrorKind>;
import { walk, walkSync, WalkOptions, WalkInfo } from "./walk.ts";
import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";

Expand Down Expand Up @@ -235,10 +233,9 @@ testWalk(
testWalk(
async (_d: string): Promise<void> => {},
async function nonexistentRoot(): Promise<void> {
const error = (await assertThrowsAsync(async () => {
await assertThrowsAsync(async () => {
await walkArray("nonexistent");
}, DenoError)) as DenoError;
assertEquals(error.kind, ErrorKind.NotFound);
}, Deno.Err.NotFound);
}
);

Expand Down
4 changes: 2 additions & 2 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -163,7 +163,7 @@ async function serveDir(
}

async function serveFallback(req: ServerRequest, e: Error): Promise<Response> {
if (e instanceof DenoError && e.kind === ErrorKind.NotFound) {
if (e instanceof Deno.Err.NotFound) {
return {
status: 404,
body: encoder.encode("Not found")
Expand Down
2 changes: 1 addition & 1 deletion node/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
);
}
});
Expand Down
7 changes: 1 addition & 6 deletions path/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions testing/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}")];

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 1737861

Please sign in to comment.