diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-02-21 10:36:13 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-21 10:36:13 -0500 |
commit | dd8a10948195f231a6a9eb652e3f208813904ad6 (patch) | |
tree | f9a4afeb67bbead882c29c2458a5f1f99e2e42db /cli/js | |
parent | d9efb8c02a0036d755c35e8e9c88d58bd45a9e2b (diff) |
refactor: remove unneeded ErrorKinds (#3936)
Diffstat (limited to 'cli/js')
39 files changed, 408 insertions, 354 deletions
diff --git a/cli/js/buffer.ts b/cli/js/buffer.ts index 2dec9fac6..dab5a4bfc 100644 --- a/cli/js/buffer.ts +++ b/cli/js/buffer.ts @@ -7,7 +7,6 @@ import { Reader, Writer, EOF, SyncReader, SyncWriter } from "./io.ts"; import { assert } from "./util.ts"; import { TextDecoder } from "./text_encoding.ts"; -import { DenoError, ErrorKind } from "./errors.ts"; // MIN_READ is the minimum ArrayBuffer size passed to a read call by // buffer.ReadFrom. As long as the Buffer has at least MIN_READ bytes beyond @@ -162,7 +161,7 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter { /** _grow() grows the buffer to guarantee space for n more bytes. * It returns the index where bytes should be written. - * If the buffer can't grow it will throw with ErrTooLarge. + * If the buffer can't grow it will throw with Error. */ private _grow(n: number): number { const m = this.length; @@ -183,10 +182,7 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter { // don't spend all our time copying. copyBytes(this.buf, this.buf.subarray(this.off)); } else if (c > MAX_SIZE - c - n) { - throw new DenoError( - ErrorKind.TooLarge, - "The buffer cannot be grown beyond the maximum size." - ); + throw new Error("The buffer cannot be grown beyond the maximum size."); } else { // Not enough space anywhere, we need to allocate. const buf = new Uint8Array(2 * c + n); @@ -202,7 +198,7 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter { /** grow() grows the buffer's capacity, if necessary, to guarantee space for * another n bytes. After grow(n), at least n bytes can be written to the * buffer without another allocation. If n is negative, grow() will panic. If - * the buffer can't grow it will throw ErrTooLarge. + * the buffer can't grow it will throw Error. * Based on https://golang.org/pkg/bytes/#Buffer.Grow */ grow(n: number): void { @@ -215,7 +211,7 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter { /** readFrom() reads data from r until EOF and appends it to the buffer, * growing the buffer as needed. It returns the number of bytes read. If the - * buffer becomes too large, readFrom will panic with ErrTooLarge. + * buffer becomes too large, readFrom will panic with Error. * Based on https://golang.org/pkg/bytes/#Buffer.ReadFrom */ async readFrom(r: Reader): Promise<number> { diff --git a/cli/js/buffer_test.ts b/cli/js/buffer_test.ts index 72c0cab4b..769b46521 100644 --- a/cli/js/buffer_test.ts +++ b/cli/js/buffer_test.ts @@ -3,7 +3,7 @@ // This code has been ported almost directly from Go's src/bytes/buffer_test.go // Copyright 2009 The Go Authors. All rights reserved. BSD license. // https://github.com/golang/go/blob/master/LICENSE -import { assert, assertEquals, test } from "./test_util.ts"; +import { assertEquals, assert, assertStrContains, test } from "./test_util.ts"; const { Buffer, readAll, readAllSync, writeAll, writeAllSync } = Deno; type Buffer = Deno.Buffer; @@ -159,8 +159,8 @@ test(async function bufferTooLargeByteWrites(): Promise<void> { err = e; } - assertEquals(err.kind, Deno.ErrorKind.TooLarge); - assertEquals(err.name, "TooLarge"); + assert(err instanceof Error); + assertStrContains(err.message, "grown beyond the maximum size"); }); test(async function bufferLargeByteReads(): Promise<void> { diff --git a/cli/js/chmod_test.ts b/cli/js/chmod_test.ts index b3b0a2ae2..24c69db7a 100644 --- a/cli/js/chmod_test.ts +++ b/cli/js/chmod_test.ts @@ -60,8 +60,7 @@ testPerm({ write: true }, function chmodSyncFailure(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: false }, function chmodSyncPerm(): void { @@ -71,7 +70,7 @@ testPerm({ write: false }, function chmodSyncPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -134,8 +133,7 @@ testPerm({ write: true }, async function chmodFailure(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: false }, async function chmodPerm(): Promise<void> { @@ -145,6 +143,6 @@ testPerm({ write: false }, async function chmodPerm(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); diff --git a/cli/js/chown_test.ts b/cli/js/chown_test.ts index 61132ae31..a1c12860d 100644 --- a/cli/js/chown_test.ts +++ b/cli/js/chown_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { testPerm, assertEquals } from "./test_util.ts"; +import { testPerm, assertEquals, assert } from "./test_util.ts"; // chown on Windows is noop for now, so ignore its testing on Windows if (Deno.build.os !== "win") { @@ -31,8 +31,7 @@ if (Deno.build.os !== "win") { try { await Deno.chown(filePath, 1000, 1000); } catch (e) { - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } }); @@ -45,8 +44,7 @@ if (Deno.build.os !== "win") { try { Deno.chownSync(filePath, uid, gid); } catch (e) { - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } } ); @@ -60,8 +58,7 @@ if (Deno.build.os !== "win") { try { await Deno.chown(filePath, uid, gid); } catch (e) { - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } } ); @@ -77,8 +74,7 @@ if (Deno.build.os !== "win") { // try changing the file's owner to root Deno.chownSync(filePath, 0, 0); } catch (e) { - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } Deno.removeSync(dirPath, { recursive: true }); }); @@ -96,8 +92,7 @@ if (Deno.build.os !== "win") { // try changing the file's owner to root await Deno.chown(filePath, 0, 0); } catch (e) { - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } await Deno.remove(dirPath, { recursive: true }); }); diff --git a/cli/js/copy_file_test.ts b/cli/js/copy_file_test.ts index 88dc3fdbf..567b246b7 100644 --- a/cli/js/copy_file_test.ts +++ b/cli/js/copy_file_test.ts @@ -43,8 +43,7 @@ testPerm({ write: true, read: true }, function copyFileSyncFailure(): void { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: true, read: false }, function copyFileSyncPerm1(): void { @@ -53,8 +52,7 @@ testPerm({ write: true, read: false }, function copyFileSyncPerm1(): void { Deno.copyFileSync("/from.txt", "/to.txt"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -65,8 +63,7 @@ testPerm({ write: false, read: true }, function copyFileSyncPerm2(): void { Deno.copyFileSync("/from.txt", "/to.txt"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -113,8 +110,7 @@ testPerm({ read: true, write: true }, async function copyFileFailure(): Promise< err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm( @@ -142,8 +138,7 @@ testPerm({ read: false, write: true }, async function copyFilePerm1(): Promise< await Deno.copyFile("/from.txt", "/to.txt"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -156,8 +151,7 @@ testPerm({ read: true, write: false }, async function copyFilePerm2(): Promise< await Deno.copyFile("/from.txt", "/to.txt"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); diff --git a/cli/js/deno.ts b/cli/js/deno.ts index cdfedcd76..8ccca9096 100644 --- a/cli/js/deno.ts +++ b/cli/js/deno.ts @@ -22,7 +22,7 @@ export { } from "./diagnostics.ts"; export { chdir, cwd } from "./dir.ts"; export { applySourceMap } from "./error_stack.ts"; -export { ErrorKind, DenoError } from "./errors.ts"; +export { Err } from "./errors.ts"; export { FileInfo } from "./file_info.ts"; export { File, diff --git a/cli/js/dir_test.ts b/cli/js/dir_test.ts index 4b03eaedd..f936986aa 100644 --- a/cli/js/dir_test.ts +++ b/cli/js/dir_test.ts @@ -29,7 +29,7 @@ testPerm({ write: true }, function dirCwdError(): void { Deno.cwd(); throw Error("current directory removed, should throw error"); } catch (err) { - if (err instanceof Deno.DenoError) { + if (err instanceof Deno.Err.NotFound) { console.log(err.name === "NotFound"); } else { throw Error("raised different exception"); @@ -45,7 +45,7 @@ testPerm({ write: true }, function dirChdirError(): void { Deno.chdir(path); throw Error("directory not available, should throw error"); } catch (err) { - if (err instanceof Deno.DenoError) { + if (err instanceof Deno.Err.NotFound) { console.log(err.name === "NotFound"); } else { throw Error("raised different exception"); diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts index adccb69c6..b0859e1ba 100644 --- a/cli/js/dispatch_json.ts +++ b/cli/js/dispatch_json.ts @@ -2,7 +2,7 @@ import * as util from "./util.ts"; import { TextEncoder, TextDecoder } from "./text_encoding.ts"; import { core } from "./core.ts"; -import { ErrorKind, DenoError } from "./errors.ts"; +import { ErrorKind, constructError } from "./errors.ts"; // eslint-disable-next-line @typescript-eslint/no-explicit-any type Ok = any; @@ -37,7 +37,7 @@ function encode(args: object): Uint8Array { function unwrapResponse(res: JsonResponse): Ok { if (res.err != null) { - throw new DenoError(res.err!.kind, res.err!.message); + return constructError(res.err!.kind, res.err!.message); } util.assert(res.ok != null); return res.ok; diff --git a/cli/js/dispatch_json_test.ts b/cli/js/dispatch_json_test.ts index c3fd6945b..90f93746e 100644 --- a/cli/js/dispatch_json_test.ts +++ b/cli/js/dispatch_json_test.ts @@ -2,7 +2,6 @@ import { test, testPerm, assert, - assertEquals, assertMatch, unreachable } from "./test_util.ts"; @@ -31,5 +30,4 @@ test(async function malformedJsonControlBuffer(): Promise<void> { const resJson = JSON.parse(resText) as any; assert(!resJson.ok); assert(resJson.err); - assertEquals(resJson.err!.kind, Deno.ErrorKind.InvalidInput); }); diff --git a/cli/js/dispatch_minimal.ts b/cli/js/dispatch_minimal.ts index 1ce3fbaef..203701085 100644 --- a/cli/js/dispatch_minimal.ts +++ b/cli/js/dispatch_minimal.ts @@ -2,7 +2,7 @@ import * as util from "./util.ts"; import { core } from "./core.ts"; import { TextDecoder } from "./text_encoding.ts"; -import { ErrorKind, DenoError } from "./errors.ts"; +import { Err, ErrorKind, constructError } from "./errors.ts"; const promiseTableMin = new Map<number, util.Resolvable<RecordMinimal>>(); // Note it's important that promiseId starts at 1 instead of 0, because sync @@ -43,7 +43,7 @@ export function recordFromBufMinimal(ui8: Uint8Array): RecordMinimal { const message = decoder.decode(ui8.slice(12)); err = { kind, message }; } else if (ui8.length != 12) { - err = { kind: ErrorKind.InvalidData, message: "Bad message" }; + throw new Err.InvalidData("BadMessage"); } return { @@ -56,7 +56,7 @@ export function recordFromBufMinimal(ui8: Uint8Array): RecordMinimal { function unwrapResponse(res: RecordMinimal): number { if (res.err != null) { - throw new DenoError(res.err!.kind, res.err!.message); + return constructError(res.err!.kind, res.err!.message); } return res.result; } diff --git a/cli/js/dispatch_minimal_test.ts b/cli/js/dispatch_minimal_test.ts index 12c70d375..75381204f 100644 --- a/cli/js/dispatch_minimal_test.ts +++ b/cli/js/dispatch_minimal_test.ts @@ -36,9 +36,7 @@ test(async function malformedMinimalControlBuffer(): Promise<void> { header.byteLength / 4 ); const arg = buf32[1]; - const result = buf32[2]; const message = new TextDecoder().decode(res.slice(12)).trim(); assert(arg < 0); - assertEquals(result, Deno.ErrorKind.InvalidInput); assertEquals(message, "Unparsable control buffer"); }); diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 0f75453ce..0844b0a56 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -1,27 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -/** A Deno specific error. The `kind` property is set to a specific error code - * which can be used to in application logic. - * - * try { - * somethingThatMightThrow(); - * } catch (e) { - * if ( - * e instanceof Deno.DenoError && - * e.kind === Deno.ErrorKind.NotFound - * ) { - * console.error("NotFound error!"); - * } - * } - * - */ -export class DenoError<T extends ErrorKind> extends Error { - constructor(readonly kind: T, msg: string) { - super(msg); - this.name = ErrorKind[kind]; - } -} - // Warning! The values in this enum are duplicated in cli/msg.rs // Update carefully! export enum ErrorKind { @@ -35,22 +13,189 @@ export enum ErrorKind { AddrNotAvailable = 8, BrokenPipe = 9, AlreadyExists = 10, - WouldBlock = 11, - InvalidInput = 12, InvalidData = 13, TimedOut = 14, Interrupted = 15, WriteZero = 16, - Other = 17, - UnexpectedEof = 18, - BadResource = 19, - UrlParse = 20, - Http = 21, - TooLarge = 22, - InvalidSeekMode = 23, - UnixError = 24, - InvalidPath = 25, - ImportPrefixMissing = 26, - Diagnostic = 27, - JSError = 28 + UnexpectedEof = 17, + BadResource = 18, + Http = 19, + URIError = 20, + TypeError = 21, + Other = 22 +} + +export function constructError(kind: ErrorKind, msg: string): never { + switch (kind) { + case ErrorKind.TypeError: + throw new TypeError(msg); + case ErrorKind.Other: + throw new Error(msg); + case ErrorKind.URIError: + throw new URIError(msg); + case ErrorKind.NotFound: + throw new NotFound(msg); + case ErrorKind.PermissionDenied: + throw new PermissionDenied(msg); + case ErrorKind.ConnectionRefused: + throw new ConnectionRefused(msg); + case ErrorKind.ConnectionReset: + throw new ConnectionReset(msg); + case ErrorKind.ConnectionAborted: + throw new ConnectionAborted(msg); + case ErrorKind.NotConnected: + throw new NotConnected(msg); + case ErrorKind.AddrInUse: + throw new AddrInUse(msg); + case ErrorKind.AddrNotAvailable: + throw new AddrNotAvailable(msg); + case ErrorKind.BrokenPipe: + throw new BrokenPipe(msg); + case ErrorKind.AlreadyExists: + throw new AlreadyExists(msg); + case ErrorKind.InvalidData: + throw new InvalidData(msg); + case ErrorKind.TimedOut: + throw new TimedOut(msg); + case ErrorKind.Interrupted: + throw new Interrupted(msg); + case ErrorKind.WriteZero: + throw new WriteZero(msg); + case ErrorKind.UnexpectedEof: + throw new UnexpectedEof(msg); + case ErrorKind.BadResource: + throw new BadResource(msg); + case ErrorKind.Http: + throw new Http(msg); + } +} + +class NotFound extends Error { + constructor(msg: string) { + super(msg); + this.name = "NotFound"; + } +} +class PermissionDenied extends Error { + constructor(msg: string) { + super(msg); + this.name = "PermissionDenied"; + } +} +class ConnectionRefused extends Error { + constructor(msg: string) { + super(msg); + this.name = "ConnectionRefused"; + } +} +class ConnectionReset extends Error { + constructor(msg: string) { + super(msg); + this.name = "ConnectionReset"; + } +} +class ConnectionAborted extends Error { + constructor(msg: string) { + super(msg); + this.name = "ConnectionAborted"; + } +} +class NotConnected extends Error { + constructor(msg: string) { + super(msg); + this.name = "NotConnected"; + } +} +class AddrInUse extends Error { + constructor(msg: string) { + super(msg); + this.name = "AddrInUse"; + } +} +class AddrNotAvailable extends Error { + constructor(msg: string) { + super(msg); + this.name = "AddrNotAvailable"; + } } +class BrokenPipe extends Error { + constructor(msg: string) { + super(msg); + this.name = "BrokenPipe"; + } +} +class AlreadyExists extends Error { + constructor(msg: string) { + super(msg); + this.name = "AlreadyExists"; + } +} +class InvalidData extends Error { + constructor(msg: string) { + super(msg); + this.name = "InvalidData"; + } +} +class TimedOut extends Error { + constructor(msg: string) { + super(msg); + this.name = "TimedOut"; + } +} +class Interrupted extends Error { + constructor(msg: string) { + super(msg); + this.name = "Interrupted"; + } +} +class WriteZero extends Error { + constructor(msg: string) { + super(msg); + this.name = "WriteZero"; + } +} +class Other extends Error { + constructor(msg: string) { + super(msg); + this.name = "Other"; + } +} +class UnexpectedEof extends Error { + constructor(msg: string) { + super(msg); + this.name = "UnexpectedEof"; + } +} +class BadResource extends Error { + constructor(msg: string) { + super(msg); + this.name = "BadResource"; + } +} +class Http extends Error { + constructor(msg: string) { + super(msg); + this.name = "Http"; + } +} + +export const Err = { + NotFound: NotFound, + PermissionDenied: PermissionDenied, + ConnectionRefused: ConnectionRefused, + ConnectionReset: ConnectionReset, + ConnectionAborted: ConnectionAborted, + NotConnected: NotConnected, + AddrInUse: AddrInUse, + AddrNotAvailable: AddrNotAvailable, + BrokenPipe: BrokenPipe, + AlreadyExists: AlreadyExists, + InvalidData: InvalidData, + TimedOut: TimedOut, + Interrupted: Interrupted, + WriteZero: WriteZero, + Other: Other, + UnexpectedEof: UnexpectedEof, + BadResource: BadResource, + Http: Http +}; diff --git a/cli/js/event_target.ts b/cli/js/event_target.ts index 495c8a042..daa73eb23 100644 --- a/cli/js/event_target.ts +++ b/cli/js/event_target.ts @@ -1,6 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as domTypes from "./dom_types.ts"; -import { DenoError, ErrorKind } from "./errors.ts"; import { hasOwnProperty, requiredArguments } from "./util.ts"; import { getRoot, @@ -134,17 +133,15 @@ export class EventTarget implements domTypes.EventTarget { } if (event.dispatched || !event.initialized) { - throw new DenoError( - ErrorKind.InvalidData, - "Tried to dispatch an uninitialized event" - ); + // TODO(bartlomieju): very likely that different error + // should be thrown here (DOMException?) + throw new TypeError("Tried to dispatch an uninitialized event"); } if (event.eventPhase !== domTypes.EventPhase.NONE) { - throw new DenoError( - ErrorKind.InvalidData, - "Tried to dispatch a dispatching event" - ); + // TODO(bartlomieju): very likely that different error + // should be thrown here (DOMException?) + throw new TypeError("Tried to dispatch a dispatching event"); } return eventTargetHelpers.dispatch(this_, event); @@ -418,7 +415,9 @@ const eventTargetHelpers = { try { listener.handleEvent(eventImpl); } catch (error) { - throw new DenoError(ErrorKind.Interrupted, error.message); + // TODO(bartlomieju): very likely that different error + // should be thrown here (DOMException?) + throw new Error(error.message); } eventImpl.inPassiveListener = false; diff --git a/cli/js/fetch_test.ts b/cli/js/fetch_test.ts index c63d05501..2912d5d2d 100644 --- a/cli/js/fetch_test.ts +++ b/cli/js/fetch_test.ts @@ -16,8 +16,7 @@ testPerm({ net: true }, async function fetchConnectionError(): Promise<void> { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.Http); - assertEquals(err.name, "Http"); + assert(err instanceof Deno.Err.Http); assertStrContains(err.message, "error trying to connect"); }); @@ -34,7 +33,7 @@ test(async function fetchPerm(): Promise<void> { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -107,8 +106,7 @@ testPerm({ net: true }, async function fetchEmptyInvalid(): Promise<void> { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.UrlParse); - assertEquals(err.name, "UrlParse"); + assert(err instanceof URIError); }); testPerm({ net: true }, async function fetchMultipartFormDataSuccess(): Promise< diff --git a/cli/js/files_test.ts b/cli/js/files_test.ts index 03e4d00e9..f71bfecc0 100644 --- a/cli/js/files_test.ts +++ b/cli/js/files_test.ts @@ -79,7 +79,7 @@ testPerm({ write: false }, async function writePermFailure(): Promise<void> { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); } }); @@ -136,8 +136,7 @@ testPerm({ read: false }, async function readPermFailure(): Promise<void> { await Deno.open("cli/tests/fixture.json", "r"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -209,7 +208,7 @@ testPerm( err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); } } @@ -380,8 +379,8 @@ testPerm({ read: true }, async function seekMode(): Promise<void> { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.InvalidSeekMode); - assertEquals(err.name, "InvalidSeekMode"); + assert(err instanceof TypeError); + assertStrContains(err.message, "Invalid seek mode"); // We should still be able to read the file // since it is still open. diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index b5a469f0b..fda2270a8 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -602,19 +602,19 @@ declare namespace Deno { write(p: Uint8Array): Promise<number>; /** _grow() grows the buffer to guarantee space for n more bytes. * It returns the index where bytes should be written. - * If the buffer can't grow it will throw with ErrTooLarge. + * If the buffer can't grow it will throw with Error. */ private _grow; /** grow() grows the buffer's capacity, if necessary, to guarantee space for * another n bytes. After grow(n), at least n bytes can be written to the * buffer without another allocation. If n is negative, grow() will panic. If - * the buffer can't grow it will throw ErrTooLarge. + * the buffer can't grow it will throw Error. * Based on https://golang.org/pkg/bytes/#Buffer.Grow */ grow(n: number): void; /** readFrom() reads data from r until EOF and appends it to the buffer, * growing the buffer as needed. It returns the number of bytes read. If the - * buffer becomes too large, readFrom will panic with ErrTooLarge. + * buffer becomes too large, readFrom will panic with Error. * Based on https://golang.org/pkg/bytes/#Buffer.ReadFrom */ readFrom(r: Reader): Promise<number>; @@ -1204,55 +1204,64 @@ declare namespace Deno { */ export function applySourceMap(location: Location): Location; - /** A Deno specific error. The `kind` property is set to a specific error code - * which can be used to in application logic. - * - * try { - * somethingThatMightThrow(); - * } catch (e) { - * if ( - * e instanceof Deno.DenoError && - * e.kind === Deno.ErrorKind.NotFound - * ) { - * console.error("NotFound error!"); - * } - * } - * - */ - export class DenoError<T extends ErrorKind> extends Error { - readonly kind: T; - constructor(kind: T, msg: string); - } - export enum ErrorKind { - NotFound = 1, - PermissionDenied = 2, - ConnectionRefused = 3, - ConnectionReset = 4, - ConnectionAborted = 5, - NotConnected = 6, - AddrInUse = 7, - AddrNotAvailable = 8, - BrokenPipe = 9, - AlreadyExists = 10, - WouldBlock = 11, - InvalidInput = 12, - InvalidData = 13, - TimedOut = 14, - Interrupted = 15, - WriteZero = 16, - Other = 17, - UnexpectedEof = 18, - BadResource = 19, - UrlParse = 20, - Http = 21, - TooLarge = 22, - InvalidSeekMode = 23, - UnixError = 24, - InvalidPath = 25, - ImportPrefixMissing = 26, - Diagnostic = 27, - JSError = 28 + /* eslint-disable @typescript-eslint/no-unused-vars */ + namespace Err { + class NotFound extends Error { + constructor(msg: string); + } + class PermissionDenied extends Error { + constructor(msg: string); + } + class ConnectionRefused extends Error { + constructor(msg: string); + } + class ConnectionReset extends Error { + constructor(msg: string); + } + class ConnectionAborted extends Error { + constructor(msg: string); + } + class NotConnected extends Error { + constructor(msg: string); + } + class AddrInUse extends Error { + constructor(msg: string); + } + class AddrNotAvailable extends Error { + constructor(msg: string); + } + class BrokenPipe extends Error { + constructor(msg: string); + } + class AlreadyExists extends Error { + constructor(msg: string); + } + class InvalidData extends Error { + constructor(msg: string); + } + class TimedOut extends Error { + constructor(msg: string); + } + class Interrupted extends Error { + constructor(msg: string); + } + class WriteZero extends Error { + constructor(msg: string); + } + class Other extends Error { + constructor(msg: string); + } + class UnexpectedEof extends Error { + constructor(msg: string); + } + class BadResource extends Error { + constructor(msg: string); + } + class Http extends Error { + constructor(msg: string); + } } + /* eslint-enable @typescript-eslint/no-unused-vars */ /** UNSTABLE: potentially want names to overlap more with browser. * diff --git a/cli/js/link_test.ts b/cli/js/link_test.ts index aada34b84..229207137 100644 --- a/cli/js/link_test.ts +++ b/cli/js/link_test.ts @@ -43,8 +43,7 @@ testPerm({ read: true, write: true }, function linkSyncExists(): void { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.AlreadyExists); - assertEquals(err.name, "AlreadyExists"); + assert(err instanceof Deno.Err.AlreadyExists); }); testPerm({ read: true, write: true }, function linkSyncNotFound(): void { @@ -59,8 +58,7 @@ testPerm({ read: true, write: true }, function linkSyncNotFound(): void { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ read: false, write: true }, function linkSyncReadPerm(): void { @@ -70,7 +68,7 @@ testPerm({ read: false, write: true }, function linkSyncReadPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -81,7 +79,7 @@ testPerm({ read: true, write: false }, function linkSyncWritePerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); diff --git a/cli/js/make_temp_test.ts b/cli/js/make_temp_test.ts index f6bf59d32..06bfe0373 100644 --- a/cli/js/make_temp_test.ts +++ b/cli/js/make_temp_test.ts @@ -23,8 +23,7 @@ testPerm({ write: true }, function makeTempDirSyncSuccess(): void { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); test(function makeTempDirSyncPerm(): void { @@ -35,7 +34,7 @@ test(function makeTempDirSyncPerm(): void { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -61,8 +60,7 @@ testPerm({ write: true }, async function makeTempDirSuccess(): Promise<void> { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: true }, function makeTempFileSyncSuccess(): void { @@ -88,8 +86,7 @@ testPerm({ write: true }, function makeTempFileSyncSuccess(): void { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); test(function makeTempFileSyncPerm(): void { @@ -100,7 +97,7 @@ test(function makeTempFileSyncPerm(): void { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -127,6 +124,5 @@ testPerm({ write: true }, async function makeTempFileSuccess(): Promise<void> { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); diff --git a/cli/js/mkdir_test.ts b/cli/js/mkdir_test.ts index dad61c1a4..856cbdeb5 100644 --- a/cli/js/mkdir_test.ts +++ b/cli/js/mkdir_test.ts @@ -25,7 +25,7 @@ testPerm({ write: false }, function mkdirSyncPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -45,8 +45,7 @@ testPerm({ write: true }, function mkdirErrIfExists(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.AlreadyExists); - assertEquals(err.name, "AlreadyExists"); + assert(err instanceof Deno.Err.AlreadyExists); }); testPerm({ read: true, write: true }, function mkdirSyncRecursive(): void { diff --git a/cli/js/net_test.ts b/cli/js/net_test.ts index 68b1918b9..a2f086f0a 100644 --- a/cli/js/net_test.ts +++ b/cli/js/net_test.ts @@ -20,15 +20,14 @@ testPerm({ net: true }, async function netCloseWhileAccept(): Promise<void> { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.Other); + assert(err instanceof Error); assertEquals(err.message, "Listener has been closed"); }); testPerm({ net: true }, async function netConcurrentAccept(): Promise<void> { const listener = Deno.listen({ port: 4502 }); let acceptErrCount = 0; - const checkErr = (e: Deno.DenoError<Deno.ErrorKind>): void => { - assertEquals(e.kind, Deno.ErrorKind.Other); + const checkErr = (e: Error): void => { if (e.message === "Listener has been closed") { assertEquals(acceptErrCount, 1); } else if (e.message === "Another accept task is ongoing") { @@ -170,8 +169,7 @@ testPerm({ net: true }, async function netDoubleCloseRead() { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.NotConnected); - assertEquals(err.name, "NotConnected"); + assert(err instanceof Deno.Err.NotConnected); closeDeferred.resolve(); listener.close(); conn.close(); @@ -205,8 +203,7 @@ testPerm({ net: true }, async function netCloseWriteSuccess() { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.BrokenPipe); - assertEquals(err.name, "BrokenPipe"); + assert(err instanceof Deno.Err.BrokenPipe); closeDeferred.resolve(); listener.close(); conn.close(); @@ -232,8 +229,7 @@ testPerm({ net: true }, async function netDoubleCloseWrite() { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.NotConnected); - assertEquals(err.name, "NotConnected"); + assert(err instanceof Deno.Err.NotConnected); closeDeferred.resolve(); listener.close(); conn.close(); diff --git a/cli/js/os.ts b/cli/js/os.ts index 554d4f78d..275dbdf1d 100644 --- a/cli/js/os.ts +++ b/cli/js/os.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as dispatch from "./dispatch.ts"; import { sendSync } from "./dispatch_json.ts"; -import { ErrorKind } from "./errors.ts"; +import { Err } from "./errors.ts"; import * as util from "./util.ts"; /** Check if running in terminal. @@ -193,7 +193,7 @@ export function dir(kind: DirKind): string | null { try { return sendSync(dispatch.OP_GET_DIR, { kind }); } catch (error) { - if (error.kind == ErrorKind.PermissionDenied) { + if (error instanceof Err.PermissionDenied) { throw error; } return null; diff --git a/cli/js/os_test.ts b/cli/js/os_test.ts index a461ba63e..fa4bf636b 100644 --- a/cli/js/os_test.ts +++ b/cli/js/os_test.ts @@ -31,7 +31,7 @@ test(function envPermissionDenied1(): void { err = e; } assertNotEquals(err, undefined); - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -43,7 +43,7 @@ test(function envPermissionDenied2(): void { err = e; } assertNotEquals(err, undefined); - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -262,7 +262,7 @@ testPerm({ env: true }, function getDir(): void { testPerm({}, function getDirWithoutPermission(): void { assertThrows( () => Deno.dir("home"), - Deno.DenoError, + Deno.Err.PermissionDenied, `run again with the --allow-env flag` ); }); @@ -277,7 +277,7 @@ testPerm({ env: false }, function execPathPerm(): void { Deno.execPath(); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); } assert(caughtError); @@ -293,7 +293,7 @@ testPerm({ env: false }, function hostnamePerm(): void { Deno.hostname(); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); } assert(caughtError); diff --git a/cli/js/permissions_test.ts b/cli/js/permissions_test.ts index d63dddb15..6d79cfec0 100644 --- a/cli/js/permissions_test.ts +++ b/cli/js/permissions_test.ts @@ -31,18 +31,26 @@ for (const grant of knownPermissions) { } test(async function permissionInvalidName(): Promise<void> { + let thrown = false; try { // eslint-disable-next-line @typescript-eslint/no-explicit-any await Deno.permissions.query({ name: "foo" as any }); } catch (e) { - assert(e.name === "Other"); + thrown = true; + assert(e instanceof Error); + } finally { + assert(thrown); } }); test(async function permissionNetInvalidUrl(): Promise<void> { + let thrown = false; try { await Deno.permissions.query({ name: "net", url: ":" }); } catch (e) { - assert(e.name === "UrlParse"); + thrown = true; + assert(e instanceof URIError); + } finally { + assert(thrown); } }); diff --git a/cli/js/process_test.ts b/cli/js/process_test.ts index dce4d9918..51ba8bfb3 100644 --- a/cli/js/process_test.ts +++ b/cli/js/process_test.ts @@ -6,16 +6,7 @@ import { assertEquals, assertStrContains } from "./test_util.ts"; -const { - kill, - run, - DenoError, - ErrorKind, - readFile, - open, - makeTempDir, - writeFile -} = Deno; +const { kill, run, readFile, open, makeTempDir, writeFile } = Deno; test(function runPermissions(): void { let caughtError = false; @@ -23,8 +14,7 @@ test(function runPermissions(): void { Deno.run({ args: ["python", "-c", "print('hello world')"] }); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -78,8 +68,7 @@ testPerm({ run: true }, function runNotFound(): void { error = e; } assert(error !== undefined); - assert(error instanceof DenoError); - assertEquals(error.kind, ErrorKind.NotFound); + assert(error instanceof Deno.Err.NotFound); }); testPerm( @@ -332,8 +321,7 @@ if (Deno.build.os !== "win") { Deno.kill(Deno.pid, Deno.Signal.SIGCONT); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -370,8 +358,7 @@ if (Deno.build.os !== "win") { } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.InvalidInput); - assertEquals(err.name, "InvalidInput"); + assert(err instanceof TypeError); p.close(); }); diff --git a/cli/js/read_dir_test.ts b/cli/js/read_dir_test.ts index 77d2cbfe2..4496d7447 100644 --- a/cli/js/read_dir_test.ts +++ b/cli/js/read_dir_test.ts @@ -32,8 +32,7 @@ testPerm({ read: false }, function readDirSyncPerm(): void { Deno.readDirSync("tests/"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -46,7 +45,7 @@ testPerm({ read: true }, function readDirSyncNotDir(): void { src = Deno.readDirSync("cli/tests/fixture.json"); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.Other); + assert(err instanceof Error); } assert(caughtError); assertEquals(src, undefined); @@ -60,7 +59,7 @@ testPerm({ read: true }, function readDirSyncNotFound(): void { src = Deno.readDirSync("bad_dir_name"); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.NotFound); + assert(err instanceof Deno.Err.NotFound); } assert(caughtError); assertEquals(src, undefined); @@ -77,8 +76,7 @@ testPerm({ read: false }, async function readDirPerm(): Promise<void> { await Deno.readDir("tests/"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); diff --git a/cli/js/read_file_test.ts b/cli/js/read_file_test.ts index d40ea1b7b..726f08413 100644 --- a/cli/js/read_file_test.ts +++ b/cli/js/read_file_test.ts @@ -16,8 +16,7 @@ testPerm({ read: false }, function readFileSyncPerm(): void { Deno.readFileSync("cli/tests/fixture.json"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -29,7 +28,7 @@ testPerm({ read: true }, function readFileSyncNotFound(): void { data = Deno.readFileSync("bad_filename"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); assert(data === undefined); @@ -50,8 +49,7 @@ testPerm({ read: false }, async function readFilePerm(): Promise<void> { await Deno.readFile("cli/tests/fixture.json"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); diff --git a/cli/js/read_link_test.ts b/cli/js/read_link_test.ts index c8db1cb58..6f028c08d 100644 --- a/cli/js/read_link_test.ts +++ b/cli/js/read_link_test.ts @@ -21,8 +21,7 @@ testPerm({ read: false }, async function readlinkSyncPerm(): Promise<void> { Deno.readlinkSync("/symlink"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -34,7 +33,7 @@ testPerm({ read: true }, function readlinkSyncNotFound(): void { data = Deno.readlinkSync("bad_filename"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); assertEquals(data, undefined); @@ -62,8 +61,7 @@ testPerm({ read: false }, async function readlinkPerm(): Promise<void> { await Deno.readlink("/symlink"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); diff --git a/cli/js/realpath_test.ts b/cli/js/realpath_test.ts index 7443c7897..6dfec45fc 100644 --- a/cli/js/realpath_test.ts +++ b/cli/js/realpath_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { testPerm, assert, assertEquals } from "./test_util.ts"; +import { testPerm, assert } from "./test_util.ts"; testPerm({ read: true }, function realpathSyncSuccess(): void { const incompletePath = "cli/tests/fixture.json"; @@ -31,8 +31,7 @@ testPerm({ read: false }, function realpathSyncPerm(): void { Deno.realpathSync("some_file"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -43,7 +42,7 @@ testPerm({ read: true }, function realpathSyncNotFound(): void { Deno.realpathSync("bad_filename"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); }); @@ -81,8 +80,7 @@ testPerm({ read: false }, async function realpathPerm(): Promise<void> { await Deno.realpath("some_file"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -93,7 +91,7 @@ testPerm({ read: true }, async function realpathNotFound(): Promise<void> { await Deno.realpath("bad_filename"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); }); diff --git a/cli/js/remove_test.ts b/cli/js/remove_test.ts index d686d1314..cb89ea1a9 100644 --- a/cli/js/remove_test.ts +++ b/cli/js/remove_test.ts @@ -18,8 +18,7 @@ testPerm({ write: true, read: true }, function removeSyncDirSuccess(): void { err = e; } // Directory is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: true, read: true }, function removeSyncFileSuccess(): void { @@ -39,8 +38,7 @@ testPerm({ write: true, read: true }, function removeSyncFileSuccess(): void { err = e; } // File is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: true, read: true }, function removeSyncFail(): void { @@ -61,8 +59,7 @@ testPerm({ write: true, read: true }, function removeSyncFail(): void { err = e; } // TODO(ry) Is Other really the error we should get here? What would Go do? - assertEquals(err.kind, Deno.ErrorKind.Other); - assertEquals(err.name, "Other"); + assert(err instanceof Error); // NON-EXISTENT DIRECTORY/FILE try { // Non-existent @@ -70,8 +67,7 @@ testPerm({ write: true, read: true }, function removeSyncFail(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm( @@ -86,7 +82,6 @@ testPerm( errOnWindows = err; } if (Deno.build.os === "win") { - assertEquals(errOnWindows.kind, Deno.ErrorKind.Other); assertEquals(errOnWindows.message, "Not implemented"); } else { const pathInfo = Deno.lstatSync(danglingSymlinkPath); @@ -98,8 +93,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } } ); @@ -121,7 +115,6 @@ testPerm( errOnWindows = err; } if (Deno.build.os === "win") { - assertEquals(errOnWindows.kind, Deno.ErrorKind.Other); assertEquals(errOnWindows.message, "Not implemented"); } else { const symlinkPathInfo = Deno.statSync(validSymlinkPath); @@ -134,8 +127,7 @@ testPerm( err = e; } Deno.removeSync(filePath); - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } } ); @@ -147,7 +139,7 @@ testPerm({ write: false }, function removeSyncPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -166,8 +158,8 @@ testPerm({ write: true, read: true }, function removeAllSyncDirSuccess(): void { err = e; } // Directory is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); + // REMOVE NON-EMPTY DIRECTORY path = Deno.makeTempDirSync() + "/dir/subdir"; const subPath = path + "/subsubdir"; @@ -185,8 +177,7 @@ testPerm({ write: true, read: true }, function removeAllSyncDirSuccess(): void { err = e; } // Directory is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm( @@ -208,8 +199,7 @@ testPerm( err = e; } // File is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } ); @@ -222,8 +212,7 @@ testPerm({ write: true }, function removeAllSyncFail(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: false }, function removeAllSyncPerm(): void { @@ -233,7 +222,7 @@ testPerm({ write: false }, function removeAllSyncPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -256,8 +245,7 @@ testPerm( err = e; } // Directory is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } ); @@ -280,8 +268,7 @@ testPerm( err = e; } // File is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } ); @@ -304,8 +291,7 @@ testPerm({ write: true, read: true }, async function removeFail(): Promise< } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.Other); - assertEquals(err.name, "Other"); + assert(err instanceof Error); // NON-EXISTENT DIRECTORY/FILE try { // Non-existent @@ -313,8 +299,7 @@ testPerm({ write: true, read: true }, async function removeFail(): Promise< } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm( @@ -329,7 +314,6 @@ testPerm( errOnWindows = e; } if (Deno.build.os === "win") { - assertEquals(errOnWindows.kind, Deno.ErrorKind.Other); assertEquals(errOnWindows.message, "Not implemented"); } else { const pathInfo = Deno.lstatSync(danglingSymlinkPath); @@ -341,8 +325,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } } ); @@ -364,7 +347,6 @@ testPerm( errOnWindows = e; } if (Deno.build.os === "win") { - assertEquals(errOnWindows.kind, Deno.ErrorKind.Other); assertEquals(errOnWindows.message, "Not implemented"); } else { const symlinkPathInfo = Deno.statSync(validSymlinkPath); @@ -377,8 +359,7 @@ testPerm( err = e; } Deno.removeSync(filePath); - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } } ); @@ -390,7 +371,7 @@ testPerm({ write: false }, async function removePerm(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -411,8 +392,8 @@ testPerm( err = e; } // Directory is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); + // REMOVE NON-EMPTY DIRECTORY path = Deno.makeTempDirSync() + "/dir/subdir"; const subPath = path + "/subsubdir"; @@ -430,8 +411,7 @@ testPerm( err = e; } // Directory is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } ); @@ -454,8 +434,7 @@ testPerm( err = e; } // File is gone - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } ); @@ -468,8 +447,7 @@ testPerm({ write: true }, async function removeAllFail(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); }); testPerm({ write: false }, async function removeAllPerm(): Promise<void> { @@ -479,6 +457,6 @@ testPerm({ write: false }, async function removeAllPerm(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); diff --git a/cli/js/rename_test.ts b/cli/js/rename_test.ts index 9c60e4d8c..3673d8489 100644 --- a/cli/js/rename_test.ts +++ b/cli/js/rename_test.ts @@ -17,7 +17,7 @@ testPerm({ read: true, write: true }, function renameSyncSuccess(): void { oldPathInfo = Deno.statSync(oldpath); } catch (e) { caughtErr = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); } assert(caughtErr); assertEquals(oldPathInfo, undefined); @@ -32,7 +32,7 @@ testPerm({ read: false, write: true }, function renameSyncReadPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -45,7 +45,7 @@ testPerm({ read: true, write: false }, function renameSyncWritePerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -67,7 +67,7 @@ testPerm({ read: true, write: true }, async function renameSuccess(): Promise< oldPathInfo = Deno.statSync(oldpath); } catch (e) { caughtErr = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); } assert(caughtErr); assertEquals(oldPathInfo, undefined); diff --git a/cli/js/stat_test.ts b/cli/js/stat_test.ts index bce5449ac..3914f877c 100644 --- a/cli/js/stat_test.ts +++ b/cli/js/stat_test.ts @@ -23,8 +23,7 @@ testPerm({ read: false }, async function statSyncPerm(): Promise<void> { Deno.statSync("README.md"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -37,8 +36,7 @@ testPerm({ read: true }, async function statSyncNotFound(): Promise<void> { badInfo = Deno.statSync("bad_file_name"); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } assert(caughtError); @@ -65,8 +63,7 @@ testPerm({ read: false }, async function lstatSyncPerm(): Promise<void> { Deno.lstatSync("README.md"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -79,8 +76,7 @@ testPerm({ read: true }, async function lstatSyncNotFound(): Promise<void> { badInfo = Deno.lstatSync("bad_file_name"); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } assert(caughtError); @@ -107,8 +103,7 @@ testPerm({ read: false }, async function statPerm(): Promise<void> { await Deno.stat("README.md"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -121,8 +116,7 @@ testPerm({ read: true }, async function statNotFound(): Promise<void> { badInfo = await Deno.stat("bad_file_name"); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } assert(caughtError); @@ -149,8 +143,7 @@ testPerm({ read: false }, async function lstatPerm(): Promise<void> { await Deno.lstat("README.md"); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -163,8 +156,7 @@ testPerm({ read: true }, async function lstatNotFound(): Promise<void> { badInfo = await Deno.lstat("bad_file_name"); } catch (err) { caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } assert(caughtError); diff --git a/cli/js/streams/pipe-to.ts b/cli/js/streams/pipe-to.ts index 3764e605b..01608b955 100644 --- a/cli/js/streams/pipe-to.ts +++ b/cli/js/streams/pipe-to.ts @@ -19,7 +19,7 @@ // import { ReadableStreamDefaultReader } from "./readable-stream-default-reader.ts"; // import { WritableStreamDefaultWriter } from "./writable-stream-default-writer.ts"; // import { PipeOptions } from "../dom_types.ts"; -// import { DenoError, ErrorKind } from "../errors.ts"; +// import { Err } from "../errors.ts"; // // add a wrapper to handle falsy rejections // interface ErrorWrapper { @@ -50,7 +50,7 @@ // abortAlgorithm = (): void => { // // TODO this should be a DOMException, // // https://github.com/stardazed/sd-streams/blob/master/packages/streams/src/pipe-to.ts#L38 -// const error = new DenoError(ErrorKind.AbortError, "Aborted"); +// const error = new Err.Aborted("Aborted"); // const actions: Array<() => Promise<void>> = []; // if (preventAbort === false) { // actions.push(() => { diff --git a/cli/js/symlink_test.ts b/cli/js/symlink_test.ts index b25c2e9c7..b89b718b3 100644 --- a/cli/js/symlink_test.ts +++ b/cli/js/symlink_test.ts @@ -15,7 +15,6 @@ testPerm({ read: true, write: true }, function symlinkSyncSuccess(): void { } if (errOnWindows) { assertEquals(Deno.build.os, "win"); - assertEquals(errOnWindows.kind, Deno.ErrorKind.Other); assertEquals(errOnWindows.message, "Not implemented"); } else { const newNameInfoLStat = Deno.lstatSync(newname); @@ -32,7 +31,7 @@ test(function symlinkSyncPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -69,7 +68,6 @@ testPerm({ read: true, write: true }, async function symlinkSuccess(): Promise< errOnWindows = e; } if (errOnWindows) { - assertEquals(errOnWindows.kind, Deno.ErrorKind.Other); assertEquals(errOnWindows.message, "Not implemented"); } else { const newNameInfoLStat = Deno.lstatSync(newname); diff --git a/cli/js/text_encoding.ts b/cli/js/text_encoding.ts index ceb2f3fdc..0709e7123 100644 --- a/cli/js/text_encoding.ts +++ b/cli/js/text_encoding.ts @@ -27,7 +27,6 @@ import * as base64 from "./base64.ts"; import { decodeUtf8 } from "./decode_utf8.ts"; import * as domTypes from "./dom_types.ts"; import { encodeUtf8 } from "./encode_utf8.ts"; -import { DenoError, ErrorKind } from "./errors.ts"; const CONTINUE = null; const END_OF_STREAM = -1; @@ -105,10 +104,7 @@ export function atob(s: string): string { const rem = s.length % 4; if (rem === 1 || /[^+/0-9A-Za-z]/.test(s)) { // TODO: throw `DOMException` - throw new DenoError( - ErrorKind.InvalidInput, - "The string to be decoded is not correctly encoded" - ); + throw new TypeError("The string to be decoded is not correctly encoded"); } // base64-js requires length exactly times of 4 @@ -130,8 +126,7 @@ export function btoa(s: string): string { for (let i = 0; i < s.length; i++) { const charCode = s[i].charCodeAt(0); if (charCode > 0xff) { - throw new DenoError( - ErrorKind.InvalidInput, + throw new TypeError( "The string to be encoded contains characters " + "outside of the Latin1 range." ); diff --git a/cli/js/text_encoding_test.ts b/cli/js/text_encoding_test.ts index 2422f86f6..28f23511a 100644 --- a/cli/js/text_encoding_test.ts +++ b/cli/js/text_encoding_test.ts @@ -59,7 +59,7 @@ test(function btoaFailed(): void { err = e; } assert(!!err); - assertEquals(err.name, "InvalidInput"); + assert(err instanceof TypeError); }); test(function textDecoder2(): void { diff --git a/cli/js/tls_test.ts b/cli/js/tls_test.ts index ac59a2eb9..dabbb2c89 100644 --- a/cli/js/tls_test.ts +++ b/cli/js/tls_test.ts @@ -13,7 +13,7 @@ test(async function connectTLSNoPerm(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -28,7 +28,7 @@ test(async function connectTLSCertFileNoReadPerm(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -51,8 +51,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); try { Deno.listenTLS({ @@ -62,8 +61,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } ); @@ -79,7 +77,7 @@ testPerm({ net: true }, async function listenTLSNoReadPerm(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -108,8 +106,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.Other); - assertEquals(err.name, "Other"); + assert(err instanceof Error); } ); @@ -138,8 +135,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.Other); - assertEquals(err.name, "Other"); + assert(err instanceof Error); } ); diff --git a/cli/js/truncate_test.ts b/cli/js/truncate_test.ts index c8809df9e..42583354c 100644 --- a/cli/js/truncate_test.ts +++ b/cli/js/truncate_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { testPerm, assertEquals } from "./test_util.ts"; +import { testPerm, assertEquals, assert } from "./test_util.ts"; function readDataSync(name: string): string { const data = Deno.readFileSync(name); @@ -58,7 +58,7 @@ testPerm({ write: false }, function truncateSyncPerm(): void { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -69,6 +69,6 @@ testPerm({ write: false }, async function truncatePerm(): Promise<void> { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); diff --git a/cli/js/utime_test.ts b/cli/js/utime_test.ts index 72a4a6477..c7e4293bf 100644 --- a/cli/js/utime_test.ts +++ b/cli/js/utime_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { testPerm, assert, assertEquals } from "./test_util.ts"; +import { testPerm, assert } from "./test_util.ts"; // Allow 10 second difference. // Note this might not be enough for FAT (but we are not testing on such fs). @@ -78,8 +78,7 @@ testPerm({ read: true, write: true }, function utimeSyncNotFound(): void { Deno.utimeSync("/baddir", atime, mtime); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); }); @@ -93,8 +92,7 @@ testPerm({ read: true, write: false }, function utimeSyncPerm(): void { Deno.utimeSync("/some_dir", atime, mtime); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -159,8 +157,7 @@ testPerm({ read: true, write: true }, async function utimeNotFound(): Promise< await Deno.utime("/baddir", atime, mtime); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); }); @@ -176,8 +173,7 @@ testPerm({ read: true, write: false }, async function utimeSyncPerm(): Promise< await Deno.utime("/some_dir", atime, mtime); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); diff --git a/cli/js/write_file_test.ts b/cli/js/write_file_test.ts index 2b952655f..9f58f4460 100644 --- a/cli/js/write_file_test.ts +++ b/cli/js/write_file_test.ts @@ -22,8 +22,7 @@ testPerm({ write: true }, function writeFileSyncFail(): void { Deno.writeFileSync(filename, data); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); }); @@ -38,8 +37,7 @@ testPerm({ write: false }, function writeFileSyncPerm(): void { Deno.writeFileSync(filename, data); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -66,8 +64,7 @@ testPerm({ read: true, write: true }, function writeFileSyncCreate(): void { Deno.writeFileSync(filename, data, { create: false }); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); @@ -128,8 +125,7 @@ testPerm( await Deno.writeFile(filename, data); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); } @@ -147,8 +143,7 @@ testPerm({ read: true, write: false }, async function writeFilePerm(): Promise< await Deno.writeFile(filename, data); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(e.name, "PermissionDenied"); + assert(e instanceof Deno.Err.PermissionDenied); } assert(caughtError); }); @@ -180,8 +175,7 @@ testPerm({ read: true, write: true }, async function writeFileCreate(): Promise< await Deno.writeFile(filename, data, { create: false }); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); - assertEquals(e.name, "NotFound"); + assert(e instanceof Deno.Err.NotFound); } assert(caughtError); |