From 8dc3bdebb47da4923e13a18cf18308d7c03821b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 9 Feb 2020 11:54:18 +0100 Subject: [PATCH 01/26] use Error and URIError --- cli/js/dispatch_json.ts | 4 ++-- cli/js/dispatch_minimal.ts | 4 ++-- cli/js/errors.ts | 13 +++++++++++++ cli/js/fetch_test.ts | 3 +-- cli/js/net_test.ts | 3 +-- cli/js/permissions_test.ts | 12 ++++++++++-- cli/js/read_dir_test.ts | 2 +- cli/js/remove_test.ts | 10 ++-------- cli/js/symlink_test.ts | 2 -- cli/js/tls_test.ts | 6 ++---- cli/tests/error_011_bad_module_specifier.ts.out | 2 +- .../error_012_bad_dynamic_import_specifier.ts.out | 2 +- cli/tests/error_type_definitions.ts.out | 2 +- 13 files changed, 37 insertions(+), 28 deletions(-) diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts index adccb69c6e7830..337070da3f387e 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, deserializeError } 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); + throw deserializeError(res.err!.kind, res.err!.message); } util.assert(res.ok != null); return res.ok; diff --git a/cli/js/dispatch_minimal.ts b/cli/js/dispatch_minimal.ts index 1ce3fbaef7cc5d..d225eed5b536e0 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 { ErrorKind, deserializeError } from "./errors.ts"; const promiseTableMin = new Map>(); // Note it's important that promiseId starts at 1 instead of 0, because sync @@ -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); + throw deserializeError(res.err!.kind, res.err!.message); } return res.result; } diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 0f75453ceaefc6..acfd77b642e633 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -1,5 +1,18 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +export function deserializeError(kind: ErrorKind, msg: string): Error { + switch (kind) { + case ErrorKind.Other: + return new Error(msg); + case ErrorKind.UrlParse: + case ErrorKind.InvalidPath: + case ErrorKind.ImportPrefixMissing: + return new URIError(msg); + default: + return new DenoError(kind, msg); + } +} + /** A Deno specific error. The `kind` property is set to a specific error code * which can be used to in application logic. * diff --git a/cli/js/fetch_test.ts b/cli/js/fetch_test.ts index 2b3d03a38c46f1..43e35a2d9c7c92 100644 --- a/cli/js/fetch_test.ts +++ b/cli/js/fetch_test.ts @@ -107,8 +107,7 @@ testPerm({ net: true }, async function fetchEmptyInvalid(): Promise { } 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/net_test.ts b/cli/js/net_test.ts index 7f5334df33445a..f3119cce16ebac 100644 --- a/cli/js/net_test.ts +++ b/cli/js/net_test.ts @@ -21,7 +21,7 @@ testPerm({ net: true }, async function netCloseWhileAccept(): Promise { err = e; } assert(!!err); - assertEquals(err.kind, Deno.ErrorKind.Other); + assert(err instanceof Error); assertEquals(err.message, "Listener has been closed"); }); @@ -29,7 +29,6 @@ testPerm({ net: true }, async function netConcurrentAccept(): Promise { const listener = Deno.listen({ port: 4502 }); let acceptErrCount = 0; const checkErr = (e): void => { - assertEquals(e.kind, Deno.ErrorKind.Other); if (e.message === "Listener has been closed") { assertEquals(acceptErrCount, 1); } else if (e.message === "Another accept task is ongoing") { diff --git a/cli/js/permissions_test.ts b/cli/js/permissions_test.ts index d63dddb15ca2d0..6d79cfec0f6549 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 { + 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 { + 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/read_dir_test.ts b/cli/js/read_dir_test.ts index 77d2cbfe256d60..e3fa9b32d80037 100644 --- a/cli/js/read_dir_test.ts +++ b/cli/js/read_dir_test.ts @@ -46,7 +46,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); diff --git a/cli/js/remove_test.ts b/cli/js/remove_test.ts index d686d1314bba16..6b9e40b5ffe90e 100644 --- a/cli/js/remove_test.ts +++ b/cli/js/remove_test.ts @@ -61,8 +61,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 @@ -86,7 +85,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); @@ -121,7 +119,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); @@ -304,8 +301,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 @@ -329,7 +325,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); @@ -364,7 +359,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); diff --git a/cli/js/symlink_test.ts b/cli/js/symlink_test.ts index b25c2e9c79fd12..d5d2223ad6e692 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); @@ -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/tls_test.ts b/cli/js/tls_test.ts index de841f5a1aaf05..95b0fa2faa6f9a 100644 --- a/cli/js/tls_test.ts +++ b/cli/js/tls_test.ts @@ -109,8 +109,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.Other); - assertEquals(err.name, "Other"); + assert(err instanceof Error); } ); @@ -139,8 +138,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.Other); - assertEquals(err.name, "Other"); + assert(err instanceof Error); } ); diff --git a/cli/tests/error_011_bad_module_specifier.ts.out b/cli/tests/error_011_bad_module_specifier.ts.out index 7c100db13ad2cf..49b1af018e493b 100644 --- a/cli/tests/error_011_bad_module_specifier.ts.out +++ b/cli/tests/error_011_bad_module_specifier.ts.out @@ -1,4 +1,4 @@ -[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_011_bad_module_specifier.ts" +[WILDCARD]error: Uncaught URIError: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_011_bad_module_specifier.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] at DenoError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_012_bad_dynamic_import_specifier.ts.out b/cli/tests/error_012_bad_dynamic_import_specifier.ts.out index 095ca497bfc2c6..50e42037d7d5f1 100644 --- a/cli/tests/error_012_bad_dynamic_import_specifier.ts.out +++ b/cli/tests/error_012_bad_dynamic_import_specifier.ts.out @@ -1,4 +1,4 @@ -[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_012_bad_dynamic_import_specifier.ts" +[WILDCARD]error: Uncaught URIError: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_012_bad_dynamic_import_specifier.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] at DenoError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_type_definitions.ts.out b/cli/tests/error_type_definitions.ts.out index d2c6096aced33c..937c0a4878e094 100644 --- a/cli/tests/error_type_definitions.ts.out +++ b/cli/tests/error_type_definitions.ts.out @@ -1,4 +1,4 @@ -[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "baz" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/type_definitions/bar.d.ts" +[WILDCARD]error: Uncaught URIError: relative import path "baz" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/type_definitions/bar.d.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] at DenoError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) From d4c226eebd05d2a8789a67c00664a00ac87079bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 14:42:04 -0500 Subject: [PATCH 02/26] remove ErrorKinds: TooLarge, InvalidSeekMode --- cli/deno_error.rs | 29 +++++++++++++++++++++-------- cli/file_fetcher.rs | 2 +- cli/js/buffer.ts | 11 ++++------- cli/js/buffer_test.ts | 6 +++--- cli/js/dispatch_json.ts | 4 ++-- cli/js/dispatch_minimal.ts | 4 ++-- cli/js/errors.ts | 16 ++++++---------- cli/js/files_test.ts | 4 ++-- cli/js/lib.deno.ns.d.ts | 16 +++++++--------- cli/msg.rs | 8 +++----- cli/ops/files.rs | 2 +- std/path/glob.ts | 5 +---- 12 files changed, 53 insertions(+), 54 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 0e1a8d9cf35c6a..9130b62d8515cf 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -1,4 +1,20 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +/// This module implements error serialization; it +/// allows to serialize Rust errors to be sent to JS runtime. +/// +/// Currently it is deeply intertwined with `ErrBox` which is +/// not optimal since not every ErrBox can be "JS runtime error"; +/// eg. there's no way to throw JSError/Diagnostic from within JS runtime +/// +/// TODO: +/// - rename ErrorKind::Other to WindowError/GenericError +/// - remove ErrorKind::Diagnostic +/// - remove ErrorKind::JSError +/// - remove ErrorKind::WouldBlock +/// - possibly rename "GetErrorKind" to "RuntimeError" - it will allow +/// better semantic separation of errors which can be sent to JS runtime +/// eg. each RuntimeError is ErrBox, but not every ErrBox is RuntimeError use crate::diagnostics::Diagnostic; use crate::fmt_errors::JSError; use crate::import_map::ImportMapError; @@ -123,12 +139,7 @@ impl GetErrorKind for ImportMapError { impl GetErrorKind for ModuleResolutionError { fn kind(&self) -> ErrorKind { - use ModuleResolutionError::*; - match self { - InvalidUrl(ref err) | InvalidBaseUrl(ref err) => err.kind(), - InvalidPath(_) => ErrorKind::InvalidPath, - ImportPrefixMissing(_, _) => ErrorKind::ImportPrefixMissing, - } + ErrorKind::UrlError } } @@ -170,7 +181,7 @@ impl GetErrorKind for io::Error { impl GetErrorKind for url::ParseError { fn kind(&self) -> ErrorKind { - ErrorKind::UrlParse + ErrorKind::UrlError } } @@ -252,6 +263,8 @@ impl GetErrorKind for DlopenError { } } +// NOTE(bartlomieju): seems this is necessary - can't use ErrBox here +// TODO(bartlomieju): ultimately this should be rewritten to `RuntimeError`? impl GetErrorKind for dyn AnyError { fn kind(&self) -> ErrorKind { use self::GetErrorKind as Get; @@ -414,7 +427,7 @@ mod tests { #[test] fn test_url_error() { let err = ErrBox::from(url_error()); - assert_eq!(err.kind(), ErrorKind::UrlParse); + assert_eq!(err.kind(), ErrorKind::UrlError); assert_eq!(err.to_string(), "empty host"); } diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index de3a76eec310d6..6ce6d676f6a1f0 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -267,7 +267,7 @@ impl SourceFileFetcher { fn fetch_local_file(&self, module_url: &Url) -> Result { let filepath = module_url.to_file_path().map_err(|()| { ErrBox::from(DenoError::new( - ErrorKind::InvalidPath, + ErrorKind::UrlError, "File URL contains invalid path".to_owned(), )) })?; diff --git a/cli/js/buffer.ts b/cli/js/buffer.ts index 2dec9fac6a0656..ac1528d7c8bf9b 100644 --- a/cli/js/buffer.ts +++ b/cli/js/buffer.ts @@ -162,7 +162,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 +183,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 +199,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 +212,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 { diff --git a/cli/js/buffer_test.ts b/cli/js/buffer_test.ts index 4b5701e54a2672..f8d6495c368f0a 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 { 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; @@ -155,8 +155,8 @@ test(async function bufferTooLargeByteWrites(): Promise { err = e; } - assertEquals(err.kind, Deno.ErrorKind.TooLarge); - assertEquals(err.name, "TooLarge"); + assert(err instanceof Error); + assertStrContains(err.message, "Too large"); }); test(async function bufferLargeByteReads(): Promise { diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts index 337070da3f387e..16de3de673f28b 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, deserializeError } 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 deserializeError(res.err!.kind, res.err!.message); + throw constructError(res.err!.kind, res.err!.message); } util.assert(res.ok != null); return res.ok; diff --git a/cli/js/dispatch_minimal.ts b/cli/js/dispatch_minimal.ts index d225eed5b536e0..c3fbd11c11e680 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, deserializeError } from "./errors.ts"; +import { ErrorKind, constructError } from "./errors.ts"; const promiseTableMin = new Map>(); // Note it's important that promiseId starts at 1 instead of 0, because sync @@ -56,7 +56,7 @@ export function recordFromBufMinimal(ui8: Uint8Array): RecordMinimal { function unwrapResponse(res: RecordMinimal): number { if (res.err != null) { - throw deserializeError(res.err!.kind, res.err!.message); + throw constructError(res.err!.kind, res.err!.message); } return res.result; } diff --git a/cli/js/errors.ts b/cli/js/errors.ts index acfd77b642e633..5771d509b0eb08 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -1,12 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -export function deserializeError(kind: ErrorKind, msg: string): Error { +export function constructError(kind: ErrorKind, msg: string): Error { switch (kind) { case ErrorKind.Other: return new Error(msg); - case ErrorKind.UrlParse: - case ErrorKind.InvalidPath: - case ErrorKind.ImportPrefixMissing: + case ErrorKind.UrlError: return new URIError(msg); default: return new DenoError(kind, msg); @@ -57,13 +55,11 @@ export enum ErrorKind { Other = 17, UnexpectedEof = 18, BadResource = 19, - UrlParse = 20, Http = 21, - TooLarge = 22, - InvalidSeekMode = 23, UnixError = 24, - InvalidPath = 25, - ImportPrefixMissing = 26, Diagnostic = 27, - JSError = 28 + JSError = 28, + + TypeError = 101, + UrlError = 100 } diff --git a/cli/js/files_test.ts b/cli/js/files_test.ts index 8f4beb085fc0bc..d9581aae258bd7 100644 --- a/cli/js/files_test.ts +++ b/cli/js/files_test.ts @@ -378,8 +378,8 @@ testPerm({ read: true }, async function seekMode(): Promise { 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 b5a469f0b12590..28a12bf846c7c9 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; /** _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; @@ -1243,15 +1243,13 @@ declare namespace Deno { Other = 17, UnexpectedEof = 18, BadResource = 19, - UrlParse = 20, Http = 21, - TooLarge = 22, - InvalidSeekMode = 23, + UnixError = 24, - InvalidPath = 25, - ImportPrefixMissing = 26, Diagnostic = 27, - JSError = 28 + JSError = 28, + TypeError = 101, + UrlError = 100 } /** UNSTABLE: potentially want names to overlap more with browser. diff --git a/cli/msg.rs b/cli/msg.rs index 643c63869ab12d..101ecd1bd6e26d 100644 --- a/cli/msg.rs +++ b/cli/msg.rs @@ -25,15 +25,13 @@ pub enum ErrorKind { Other = 17, UnexpectedEof = 18, BadResource = 19, - UrlParse = 20, Http = 21, - TooLarge = 22, - InvalidSeekMode = 23, UnixError = 24, - InvalidPath = 25, - ImportPrefixMissing = 26, Diagnostic = 27, JSError = 28, + + UrlError = 100, + TypeError = 101, } // Warning! The values in this enum are duplicated in js/compiler.ts diff --git a/cli/ops/files.rs b/cli/ops/files.rs index d625d45903dae2..9619051a7143dc 100644 --- a/cli/ops/files.rs +++ b/cli/ops/files.rs @@ -190,7 +190,7 @@ fn op_seek( 2 => SeekFrom::End(i64::from(offset)), _ => { return Err(ErrBox::from(DenoError::new( - ErrorKind::InvalidSeekMode, + ErrorKind::TypeError, format!("Invalid seek mode: {}", whence), ))); } diff --git a/std/path/glob.ts b/std/path/glob.ts index a079f448f05e43..06a4ded8c08aa7 100644 --- a/std/path/glob.ts +++ b/std/path/glob.ts @@ -91,10 +91,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); From 61300e76d00d7d48487998add0a31cc04b08774e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 14:46:42 -0500 Subject: [PATCH 03/26] fix integration test output --- cli/tests/044_bad_resource.ts.out | 2 +- cli/tests/error_004_missing_module.ts.out | 2 +- cli/tests/error_005_missing_dynamic_import.ts.out | 2 +- cli/tests/error_006_import_ext_failure.ts.out | 2 +- cli/tests/error_011_bad_module_specifier.ts.out | 2 +- cli/tests/error_012_bad_dynamic_import_specifier.ts.out | 2 +- cli/tests/error_type_definitions.ts.out | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cli/tests/044_bad_resource.ts.out b/cli/tests/044_bad_resource.ts.out index 155e4396f19638..cb9d4af1af8cbe 100644 --- a/cli/tests/044_bad_resource.ts.out +++ b/cli/tests/044_bad_resource.ts.out @@ -1,6 +1,6 @@ [WILDCARD] error: Uncaught BadResource: bad resource id [WILDCARD]dispatch_json.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_004_missing_module.ts.out b/cli/tests/error_004_missing_module.ts.out index a9be1419a6b1e1..ce56cea6151b93 100644 --- a/cli/tests/error_004_missing_module.ts.out +++ b/cli/tests/error_004_missing_module.ts.out @@ -1,5 +1,5 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_004_missing_module.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_005_missing_dynamic_import.ts.out b/cli/tests/error_005_missing_dynamic_import.ts.out index 24031a61c9abd4..1312e0ab6170b5 100644 --- a/cli/tests/error_005_missing_dynamic_import.ts.out +++ b/cli/tests/error_005_missing_dynamic_import.ts.out @@ -1,5 +1,5 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_005_missing_dynamic_import.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_006_import_ext_failure.ts.out b/cli/tests/error_006_import_ext_failure.ts.out index deda91c2f762ae..b34dee0821860c 100644 --- a/cli/tests/error_006_import_ext_failure.ts.out +++ b/cli/tests/error_006_import_ext_failure.ts.out @@ -1,5 +1,5 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/non-existent" from "[WILDCARD]/error_006_import_ext_failure.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_011_bad_module_specifier.ts.out b/cli/tests/error_011_bad_module_specifier.ts.out index 49b1af018e493b..c4388a585680d0 100644 --- a/cli/tests/error_011_bad_module_specifier.ts.out +++ b/cli/tests/error_011_bad_module_specifier.ts.out @@ -1,6 +1,6 @@ [WILDCARD]error: Uncaught URIError: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_011_bad_module_specifier.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] - at DenoError ($deno$/errors.ts:[WILDCARD]) + at constructError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) at sendSync ($deno$/dispatch_json.ts:[WILDCARD]) at resolveModules ($deno$/compiler_imports.ts:[WILDCARD]) diff --git a/cli/tests/error_012_bad_dynamic_import_specifier.ts.out b/cli/tests/error_012_bad_dynamic_import_specifier.ts.out index 50e42037d7d5f1..f9127be95a8d9c 100644 --- a/cli/tests/error_012_bad_dynamic_import_specifier.ts.out +++ b/cli/tests/error_012_bad_dynamic_import_specifier.ts.out @@ -1,6 +1,6 @@ [WILDCARD]error: Uncaught URIError: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_012_bad_dynamic_import_specifier.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] - at DenoError ($deno$/errors.ts:[WILDCARD]) + at constructError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) at sendSync ($deno$/dispatch_json.ts:[WILDCARD]) at resolveModules ($deno$/compiler_imports.ts:[WILDCARD]) diff --git a/cli/tests/error_type_definitions.ts.out b/cli/tests/error_type_definitions.ts.out index 937c0a4878e094..7a372e1bd358cf 100644 --- a/cli/tests/error_type_definitions.ts.out +++ b/cli/tests/error_type_definitions.ts.out @@ -1,6 +1,6 @@ [WILDCARD]error: Uncaught URIError: relative import path "baz" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/type_definitions/bar.d.ts" [WILDCARD]dispatch_json.ts:[WILDCARD] - at DenoError ($deno$/errors.ts:[WILDCARD]) + at constructError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) at sendSync ($deno$/dispatch_json.ts:[WILDCARD]) at resolveModules ($deno$/compiler_imports.ts:[WILDCARD]) From d761e613b1513187958fbc1cf13fce135d1c526d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 14:47:33 -0500 Subject: [PATCH 04/26] lint --- cli/js/buffer.ts | 1 - std/path/glob.ts | 2 -- 2 files changed, 3 deletions(-) diff --git a/cli/js/buffer.ts b/cli/js/buffer.ts index ac1528d7c8bf9b..dab5a4bfc98281 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 diff --git a/std/path/glob.ts b/std/path/glob.ts index 06a4ded8c08aa7..8eb106b251dab3 100644 --- a/std/path/glob.ts +++ b/std/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; From 9affde9d855984fe45460fb3d1a58dce9b3240cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 15:01:31 -0500 Subject: [PATCH 05/26] remove ErrorKind::Diagnostic --- cli/deno_error.rs | 56 ------------------------------------------- cli/js/buffer_test.ts | 2 +- cli/js/errors.ts | 2 ++ 3 files changed, 3 insertions(+), 57 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 9130b62d8515cf..1010096438561b 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -9,13 +9,11 @@ /// /// TODO: /// - rename ErrorKind::Other to WindowError/GenericError -/// - remove ErrorKind::Diagnostic /// - remove ErrorKind::JSError /// - remove ErrorKind::WouldBlock /// - possibly rename "GetErrorKind" to "RuntimeError" - it will allow /// better semantic separation of errors which can be sent to JS runtime /// eg. each RuntimeError is ErrBox, but not every ErrBox is RuntimeError -use crate::diagnostics::Diagnostic; use crate::fmt_errors::JSError; use crate::import_map::ImportMapError; pub use crate::msg::ErrorKind; @@ -125,12 +123,6 @@ impl GetErrorKind for JSError { } } -impl GetErrorKind for Diagnostic { - fn kind(&self) -> ErrorKind { - ErrorKind::Diagnostic - } -} - impl GetErrorKind for ImportMapError { fn kind(&self) -> ErrorKind { ErrorKind::Other @@ -281,7 +273,6 @@ impl GetErrorKind for dyn AnyError { None .or_else(|| self.downcast_ref::().map(Get::kind)) - .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) @@ -308,9 +299,6 @@ impl GetErrorKind for dyn AnyError { mod tests { use super::*; use crate::colors::strip_ansi_codes; - use crate::diagnostics::Diagnostic; - use crate::diagnostics::DiagnosticCategory; - use crate::diagnostics::DiagnosticItem; use deno_core::ErrBox; use deno_core::StackFrame; use deno_core::V8Exception; @@ -358,43 +346,6 @@ mod tests { }) } - fn diagnostic() -> Diagnostic { - Diagnostic { - items: vec![ - DiagnosticItem { - message: "Example 1".to_string(), - message_chain: None, - code: 2322, - category: DiagnosticCategory::Error, - start_position: Some(267), - end_position: Some(273), - source_line: Some(" values: o => [".to_string()), - line_number: Some(18), - script_resource_name: Some( - "deno/tests/complex_diagnostics.ts".to_string(), - ), - start_column: Some(2), - end_column: Some(8), - related_information: None, - }, - DiagnosticItem { - message: "Example 2".to_string(), - message_chain: None, - code: 2000, - category: DiagnosticCategory::Error, - start_position: Some(2), - end_position: Some(2), - source_line: Some(" values: undefined,".to_string()), - line_number: Some(128), - script_resource_name: Some("/foo/bar.ts".to_string()), - start_column: Some(2), - end_column: Some(8), - related_information: None, - }, - ], - } - } - fn io_error() -> io::Error { io::Error::from(io::ErrorKind::NotFound) } @@ -433,13 +384,6 @@ mod tests { // TODO find a way to easily test tokio errors and unix errors - #[test] - fn test_diagnostic() { - let err = ErrBox::from(diagnostic()); - assert_eq!(err.kind(), ErrorKind::Diagnostic); - assert_eq!(strip_ansi_codes(&err.to_string()), "error TS2322: Example 1\n\n► deno/tests/complex_diagnostics.ts:19:3\n\n19 values: o => [\n ~~~~~~\n\nerror TS2000: Example 2\n\n► /foo/bar.ts:129:3\n\n129 values: undefined,\n ~~~~~~\n\n\nFound 2 errors.\n"); - } - #[test] fn test_js_error() { let err = ErrBox::from(js_error()); diff --git a/cli/js/buffer_test.ts b/cli/js/buffer_test.ts index f8d6495c368f0a..7ea4944a11cfe2 100644 --- a/cli/js/buffer_test.ts +++ b/cli/js/buffer_test.ts @@ -156,7 +156,7 @@ test(async function bufferTooLargeByteWrites(): Promise { } assert(err instanceof Error); - assertStrContains(err.message, "Too large"); + assertStrContains(err.message, "grown beyond the maximum size"); }); test(async function bufferLargeByteReads(): Promise { diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 5771d509b0eb08..944e994052b70b 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -2,6 +2,8 @@ export function constructError(kind: ErrorKind, msg: string): Error { switch (kind) { + case ErrorKind.TypeError: + return new TypeError(msg); case ErrorKind.Other: return new Error(msg); case ErrorKind.UrlError: From ba0d24972aeae7fac6f46bf4f7501400aace57a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 15:19:04 -0500 Subject: [PATCH 06/26] fix test output --- cli/js/dispatch_json.ts | 2 +- cli/js/dispatch_minimal.ts | 2 +- cli/js/errors.ts | 11 +++++------ cli/js/lib.deno.ns.d.ts | 2 -- cli/msg.rs | 1 - cli/tests/044_bad_resource.ts.out | 3 ++- cli/tests/error_004_missing_module.ts.out | 3 ++- cli/tests/error_005_missing_dynamic_import.ts.out | 3 ++- cli/tests/error_006_import_ext_failure.ts.out | 3 ++- cli/tests/error_011_bad_module_specifier.ts.out | 2 +- .../error_012_bad_dynamic_import_specifier.ts.out | 2 +- cli/tests/error_type_definitions.ts.out | 2 +- 12 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts index 16de3de673f28b..b0859e1ba31827 100644 --- a/cli/js/dispatch_json.ts +++ b/cli/js/dispatch_json.ts @@ -37,7 +37,7 @@ function encode(args: object): Uint8Array { function unwrapResponse(res: JsonResponse): Ok { if (res.err != null) { - throw constructError(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_minimal.ts b/cli/js/dispatch_minimal.ts index c3fbd11c11e680..e5f62366a83a8e 100644 --- a/cli/js/dispatch_minimal.ts +++ b/cli/js/dispatch_minimal.ts @@ -56,7 +56,7 @@ export function recordFromBufMinimal(ui8: Uint8Array): RecordMinimal { function unwrapResponse(res: RecordMinimal): number { if (res.err != null) { - throw constructError(res.err!.kind, res.err!.message); + return constructError(res.err!.kind, res.err!.message); } return res.result; } diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 944e994052b70b..4bcd70748d9e5f 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -1,15 +1,15 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -export function constructError(kind: ErrorKind, msg: string): Error { +export function constructError(kind: ErrorKind, msg: string): never { switch (kind) { case ErrorKind.TypeError: - return new TypeError(msg); + throw new TypeError(msg); case ErrorKind.Other: - return new Error(msg); + throw new Error(msg); case ErrorKind.UrlError: - return new URIError(msg); + throw new URIError(msg); default: - return new DenoError(kind, msg); + throw new DenoError(kind, msg); } } @@ -59,7 +59,6 @@ export enum ErrorKind { BadResource = 19, Http = 21, UnixError = 24, - Diagnostic = 27, JSError = 28, TypeError = 101, diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 28a12bf846c7c9..3a3be54899a1b0 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1244,9 +1244,7 @@ declare namespace Deno { UnexpectedEof = 18, BadResource = 19, Http = 21, - UnixError = 24, - Diagnostic = 27, JSError = 28, TypeError = 101, UrlError = 100 diff --git a/cli/msg.rs b/cli/msg.rs index 101ecd1bd6e26d..bcbb1e6b1c3dd9 100644 --- a/cli/msg.rs +++ b/cli/msg.rs @@ -27,7 +27,6 @@ pub enum ErrorKind { BadResource = 19, Http = 21, UnixError = 24, - Diagnostic = 27, JSError = 28, UrlError = 100, diff --git a/cli/tests/044_bad_resource.ts.out b/cli/tests/044_bad_resource.ts.out index cb9d4af1af8cbe..f2a79476f0e2d8 100644 --- a/cli/tests/044_bad_resource.ts.out +++ b/cli/tests/044_bad_resource.ts.out @@ -1,6 +1,7 @@ [WILDCARD] error: Uncaught BadResource: bad resource id -[WILDCARD]dispatch_json.ts:[WILDCARD] +[WILDCARD]errors.ts:[WILDCARD] + at DenoError ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_004_missing_module.ts.out b/cli/tests/error_004_missing_module.ts.out index ce56cea6151b93..99bddb1d9057f5 100644 --- a/cli/tests/error_004_missing_module.ts.out +++ b/cli/tests/error_004_missing_module.ts.out @@ -1,5 +1,6 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_004_missing_module.ts" -[WILDCARD]dispatch_json.ts:[WILDCARD] +[WILDCARD]errors.ts:[WILDCARD] + at DenoError ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_005_missing_dynamic_import.ts.out b/cli/tests/error_005_missing_dynamic_import.ts.out index 1312e0ab6170b5..f08e2f102ca0cc 100644 --- a/cli/tests/error_005_missing_dynamic_import.ts.out +++ b/cli/tests/error_005_missing_dynamic_import.ts.out @@ -1,5 +1,6 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_005_missing_dynamic_import.ts" -[WILDCARD]dispatch_json.ts:[WILDCARD] +[WILDCARD]errors.ts:[WILDCARD] + at DenoError ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_006_import_ext_failure.ts.out b/cli/tests/error_006_import_ext_failure.ts.out index b34dee0821860c..ba6c7f20f6460c 100644 --- a/cli/tests/error_006_import_ext_failure.ts.out +++ b/cli/tests/error_006_import_ext_failure.ts.out @@ -1,5 +1,6 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/non-existent" from "[WILDCARD]/error_006_import_ext_failure.ts" -[WILDCARD]dispatch_json.ts:[WILDCARD] +[WILDCARD]errors.ts:[WILDCARD] + at DenoError ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_011_bad_module_specifier.ts.out b/cli/tests/error_011_bad_module_specifier.ts.out index c4388a585680d0..7bf7ef26a65010 100644 --- a/cli/tests/error_011_bad_module_specifier.ts.out +++ b/cli/tests/error_011_bad_module_specifier.ts.out @@ -1,5 +1,5 @@ [WILDCARD]error: Uncaught URIError: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_011_bad_module_specifier.ts" -[WILDCARD]dispatch_json.ts:[WILDCARD] +[WILDCARD]errors.ts:[WILDCARD] at constructError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) at sendSync ($deno$/dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_012_bad_dynamic_import_specifier.ts.out b/cli/tests/error_012_bad_dynamic_import_specifier.ts.out index f9127be95a8d9c..9f41f9b2873f36 100644 --- a/cli/tests/error_012_bad_dynamic_import_specifier.ts.out +++ b/cli/tests/error_012_bad_dynamic_import_specifier.ts.out @@ -1,5 +1,5 @@ [WILDCARD]error: Uncaught URIError: relative import path "bad-module.ts" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/error_012_bad_dynamic_import_specifier.ts" -[WILDCARD]dispatch_json.ts:[WILDCARD] +[WILDCARD]errors.ts:[WILDCARD] at constructError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) at sendSync ($deno$/dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_type_definitions.ts.out b/cli/tests/error_type_definitions.ts.out index 7a372e1bd358cf..04b012eadf6a6d 100644 --- a/cli/tests/error_type_definitions.ts.out +++ b/cli/tests/error_type_definitions.ts.out @@ -1,5 +1,5 @@ [WILDCARD]error: Uncaught URIError: relative import path "baz" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/type_definitions/bar.d.ts" -[WILDCARD]dispatch_json.ts:[WILDCARD] +[WILDCARD]errors.ts:[WILDCARD] at constructError ($deno$/errors.ts:[WILDCARD]) at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD]) at sendSync ($deno$/dispatch_json.ts:[WILDCARD]) From 19e80383f02d7fc42f4c25ee883db1f99b24df9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 15:30:05 -0500 Subject: [PATCH 07/26] remove ErrorKind::JSError --- cli/deno_error.rs | 62 ----------------------------------------- cli/js/errors.ts | 1 - cli/js/lib.deno.ns.d.ts | 1 - cli/msg.rs | 1 - cli/ops/worker_host.rs | 29 +++++++++---------- 5 files changed, 15 insertions(+), 79 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 1010096438561b..c9cd597d199de3 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -9,12 +9,10 @@ /// /// TODO: /// - rename ErrorKind::Other to WindowError/GenericError -/// - remove ErrorKind::JSError /// - remove ErrorKind::WouldBlock /// - possibly rename "GetErrorKind" to "RuntimeError" - it will allow /// better semantic separation of errors which can be sent to JS runtime /// eg. each RuntimeError is ErrBox, but not every ErrBox is RuntimeError -use crate::fmt_errors::JSError; use crate::import_map::ImportMapError; pub use crate::msg::ErrorKind; use deno_core::AnyError; @@ -117,12 +115,6 @@ impl GetErrorKind for StaticError { } } -impl GetErrorKind for JSError { - fn kind(&self) -> ErrorKind { - ErrorKind::JSError - } -} - impl GetErrorKind for ImportMapError { fn kind(&self) -> ErrorKind { ErrorKind::Other @@ -276,7 +268,6 @@ impl GetErrorKind for dyn AnyError { .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) - .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) @@ -298,53 +289,7 @@ impl GetErrorKind for dyn AnyError { #[cfg(test)] mod tests { use super::*; - use crate::colors::strip_ansi_codes; use deno_core::ErrBox; - use deno_core::StackFrame; - use deno_core::V8Exception; - - fn js_error() -> JSError { - JSError::new(V8Exception { - message: "Error: foo bar".to_string(), - source_line: None, - script_resource_name: None, - line_number: None, - start_position: None, - end_position: None, - error_level: None, - start_column: None, - end_column: None, - frames: vec![ - StackFrame { - line: 4, - column: 16, - script_name: "foo_bar.ts".to_string(), - function_name: "foo".to_string(), - is_eval: false, - is_constructor: false, - is_wasm: false, - }, - StackFrame { - line: 5, - column: 20, - script_name: "bar_baz.ts".to_string(), - function_name: "qat".to_string(), - is_eval: false, - is_constructor: false, - is_wasm: false, - }, - StackFrame { - line: 1, - column: 1, - script_name: "deno_main.js".to_string(), - function_name: "".to_string(), - is_eval: false, - is_constructor: false, - is_wasm: false, - }, - ], - }) - } fn io_error() -> io::Error { io::Error::from(io::ErrorKind::NotFound) @@ -384,13 +329,6 @@ mod tests { // TODO find a way to easily test tokio errors and unix errors - #[test] - fn test_js_error() { - let err = ErrBox::from(js_error()); - assert_eq!(err.kind(), ErrorKind::JSError); - assert_eq!(strip_ansi_codes(&err.to_string()), "error: Error: foo bar\n at foo (foo_bar.ts:5:17)\n at qat (bar_baz.ts:6:21)\n at deno_main.js:2:2"); - } - #[test] fn test_import_map_error() { let err = ErrBox::from(import_map_error()); diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 4bcd70748d9e5f..587686b4414ace 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -59,7 +59,6 @@ export enum ErrorKind { BadResource = 19, Http = 21, UnixError = 24, - JSError = 28, TypeError = 101, UrlError = 100 diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 3a3be54899a1b0..7edd4e535c661c 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1245,7 +1245,6 @@ declare namespace Deno { BadResource = 19, Http = 21, UnixError = 24, - JSError = 28, TypeError = 101, UrlError = 100 } diff --git a/cli/msg.rs b/cli/msg.rs index bcbb1e6b1c3dd9..ab51f7e7bb029d 100644 --- a/cli/msg.rs +++ b/cli/msg.rs @@ -27,7 +27,6 @@ pub enum ErrorKind { BadResource = 19, Http = 21, UnixError = 24, - JSError = 28, UrlError = 100, TypeError = 101, diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs index 13d4fffffcff36..c3b90767327e5a 100644 --- a/cli/ops/worker_host.rs +++ b/cli/ops/worker_host.rs @@ -2,7 +2,6 @@ use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::deno_error::DenoError; use crate::deno_error::ErrorKind; -use crate::deno_error::GetErrorKind; use crate::fmt_errors::JSError; use crate::futures::SinkExt; use crate::global_state::GlobalState; @@ -212,11 +211,17 @@ fn op_host_terminate_worker( fn serialize_worker_event(event: WorkerEvent) -> Value { match event { WorkerEvent::Message(buf) => json!({ "type": "msg", "data": buf }), - WorkerEvent::Error(error) => match error.kind() { - ErrorKind::JSError => { - let error = error.downcast::().unwrap(); - let exception: V8Exception = error.into(); - json!({ + WorkerEvent::Error(error) => { + let mut serialized_error = json!({ + "type": "error", + "error": { + "message": error.to_string(), + } + }); + + if let Ok(err) = error.downcast::() { + let exception: V8Exception = err.into(); + serialized_error = json!({ "type": "error", "error": { "message": exception.message, @@ -224,15 +229,11 @@ fn serialize_worker_event(event: WorkerEvent) -> Value { "lineNumber": exception.line_number, "columnNumber": exception.start_column, } - }) + }); } - _ => json!({ - "type": "error", - "error": { - "message": error.to_string(), - } - }), - }, + + serialized_error + } } } From a4e6a255e03c7d0978ecf7326c1daf738dce633d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 16:52:39 -0500 Subject: [PATCH 08/26] fix after merge --- cli/js/net_test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cli/js/net_test.ts b/cli/js/net_test.ts index dbed946733d693..f78f3a25d19c9a 100644 --- a/cli/js/net_test.ts +++ b/cli/js/net_test.ts @@ -27,8 +27,7 @@ testPerm({ net: true }, async function netCloseWhileAccept(): Promise { testPerm({ net: true }, async function netConcurrentAccept(): Promise { const listener = Deno.listen({ port: 4502 }); let acceptErrCount = 0; - const checkErr = (e: Deno.DenoError): 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") { From d15f9936258910e9bcf7432cbf4af8c414396ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 16:53:21 -0500 Subject: [PATCH 09/26] rename ErrorKind::UrlError -> URIError --- cli/deno_error.rs | 6 +++--- cli/file_fetcher.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index c9cd597d199de3..c9cdf5f038751a 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -123,7 +123,7 @@ impl GetErrorKind for ImportMapError { impl GetErrorKind for ModuleResolutionError { fn kind(&self) -> ErrorKind { - ErrorKind::UrlError + ErrorKind::URIError } } @@ -165,7 +165,7 @@ impl GetErrorKind for io::Error { impl GetErrorKind for url::ParseError { fn kind(&self) -> ErrorKind { - ErrorKind::UrlError + ErrorKind::URIError } } @@ -323,7 +323,7 @@ mod tests { #[test] fn test_url_error() { let err = ErrBox::from(url_error()); - assert_eq!(err.kind(), ErrorKind::UrlError); + assert_eq!(err.kind(), ErrorKind::URIError); assert_eq!(err.to_string(), "empty host"); } diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index 6ce6d676f6a1f0..3dd184ebd764a3 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -267,7 +267,7 @@ impl SourceFileFetcher { fn fetch_local_file(&self, module_url: &Url) -> Result { let filepath = module_url.to_file_path().map_err(|()| { ErrBox::from(DenoError::new( - ErrorKind::UrlError, + ErrorKind::URIError, "File URL contains invalid path".to_owned(), )) })?; From 3b6da485e904e566d488ae72e015715c51ebf2dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 16:53:42 -0500 Subject: [PATCH 10/26] update module docstring --- cli/deno_error.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index c9cdf5f038751a..fd03157c06402f 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -1,18 +1,33 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -/// This module implements error serialization; it -/// allows to serialize Rust errors to be sent to JS runtime. -/// -/// Currently it is deeply intertwined with `ErrBox` which is -/// not optimal since not every ErrBox can be "JS runtime error"; -/// eg. there's no way to throw JSError/Diagnostic from within JS runtime -/// -/// TODO: -/// - rename ErrorKind::Other to WindowError/GenericError -/// - remove ErrorKind::WouldBlock -/// - possibly rename "GetErrorKind" to "RuntimeError" - it will allow -/// better semantic separation of errors which can be sent to JS runtime -/// eg. each RuntimeError is ErrBox, but not every ErrBox is RuntimeError +//! This module implements error serialization; it +//! allows to serialize Rust errors to be sent to JS runtime. +//! +//! Currently it is deeply intertwined with `ErrBox` which is +//! not optimal since not every ErrBox can be "JS runtime error"; +//! eg. there's no way to throw JSError/Diagnostic from within JS runtime +//! +//! There are many types of errors in Deno: +//! - ErrBox: a generic boxed object. This is the super type of all +//! errors handled in Rust. +//! - JSError: exceptions thrown from V8 into Rust. Usually a user exception. +//! These are basically a big JSON structure which holds information about +//! line numbers. We use this to pretty-print stack traces. These are +//! never passed back into the runtime. +//! - DenoError: these are errors that happen during ops, which are passed +//! back into the runtime, where an exception object is created and thrown. +//! DenoErrors have an integer code associated with them - access this via the kind() method. +//! - Diagnostic: these are errors that originate in TypeScript's compiler. +//! They're similar to JSError, in that they have line numbers. +//! But Diagnostics are compile-time type errors, whereas JSErrors are runtime exceptions. +//! +//! TODO: +//! - rename ErrorKind::Other to WindowError/GenericError +//! - remove ErrorKind::WouldBlock +//! - possibly rename "GetErrorKind" to "RuntimeError" - it will allow +//! better semantic separation of errors which can be sent to JS runtime +//! eg. each RuntimeError is ErrBox, but not every ErrBox is RuntimeError + use crate::import_map::ImportMapError; pub use crate::msg::ErrorKind; use deno_core::AnyError; From 5710a04e60d399266a2077a9996eb94d5013c244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 16:59:13 -0500 Subject: [PATCH 11/26] move ErrorKind to cli/deno_error.rs --- cli/deno_error.rs | 31 +++++++++++++++++++++++++++++++ cli/msg.rs | 32 -------------------------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index fd03157c06402f..9c7a716f498899 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -43,6 +43,37 @@ use std::fmt; use std::io; use url; +// Warning! The values in this enum are duplicated in js/errors.ts +// Update carefully! +#[allow(non_camel_case_types)] +#[repr(i8)] +#[derive(Clone, Copy, PartialEq, Debug)] +pub 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, + Http = 21, + UnixError = 24, + UrlError = 100, + TypeError = 101, +} + #[derive(Debug)] pub struct DenoError { kind: ErrorKind, diff --git a/cli/msg.rs b/cli/msg.rs index ab51f7e7bb029d..146f45ea5485ec 100644 --- a/cli/msg.rs +++ b/cli/msg.rs @@ -1,37 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -// Warning! The values in this enum are duplicated in js/errors.ts -// Update carefully! -#[allow(non_camel_case_types)] -#[repr(i8)] -#[derive(Clone, Copy, PartialEq, Debug)] -pub 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, - Http = 21, - UnixError = 24, - - UrlError = 100, - TypeError = 101, -} - // Warning! The values in this enum are duplicated in js/compiler.ts // Update carefully! #[allow(non_camel_case_types)] From ae65f8c77863f1e9f7029d7327acbf7c9c52aaf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 17:06:21 -0500 Subject: [PATCH 12/26] update todo --- cli/deno_error.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 9c7a716f498899..03db256bac6e7c 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -22,11 +22,12 @@ //! But Diagnostics are compile-time type errors, whereas JSErrors are runtime exceptions. //! //! TODO: -//! - rename ErrorKind::Other to WindowError/GenericError -//! - remove ErrorKind::WouldBlock -//! - possibly rename "GetErrorKind" to "RuntimeError" - it will allow -//! better semantic separation of errors which can be sent to JS runtime -//! eg. each RuntimeError is ErrBox, but not every ErrBox is RuntimeError +//! - rename DenoError to OpError? +//! - rename JSError to RuntimeException. merge V8Exception? +//! - remove ErrorKind::WouldBlock +//! - rename ErrorKind::Other. This corresponds to a generic exception thrown as the +//! global `Error` in JS: +//! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error use crate::import_map::ImportMapError; pub use crate::msg::ErrorKind; @@ -70,6 +71,7 @@ pub enum ErrorKind { BadResource = 19, Http = 21, UnixError = 24, + UrlError = 100, TypeError = 101, } From 7e0a36c1e01b262d529323d8cbe5fb52e2e696a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 17:16:27 -0500 Subject: [PATCH 13/26] fix ErrorKind --- cli/deno_error.rs | 3 +-- cli/ops/dispatch_minimal.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 03db256bac6e7c..8b4cbf32e11ea8 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -30,7 +30,6 @@ //! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error use crate::import_map::ImportMapError; -pub use crate::msg::ErrorKind; use deno_core::AnyError; use deno_core::ErrBox; use deno_core::ModuleResolutionError; @@ -72,7 +71,7 @@ pub enum ErrorKind { Http = 21, UnixError = 24, - UrlError = 100, + URIError = 100, TypeError = 101, } diff --git a/cli/ops/dispatch_minimal.rs b/cli/ops/dispatch_minimal.rs index 70c4af6c3895c7..762146539f4f97 100644 --- a/cli/ops/dispatch_minimal.rs +++ b/cli/ops/dispatch_minimal.rs @@ -4,8 +4,8 @@ //! alternative to flatbuffers using a very simple list of int32s to lay out //! messages. The first i32 is used to determine if a message a flatbuffer //! message or a "minimal" message. +use crate::deno_error::ErrorKind; use crate::deno_error::GetErrorKind; -use crate::msg::ErrorKind; use byteorder::{LittleEndian, WriteBytesExt}; use deno_core::Buf; use deno_core::CoreOp; From d483bdfb04db5ef51cae5d7f2ec8d1e917df9f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 17:42:09 -0500 Subject: [PATCH 14/26] remove ErrorKind::WouldBlock --- cli/compilers/ts.rs | 1 + cli/deno_error.rs | 4 +--- cli/js/errors.ts | 1 - cli/js/lib.deno.ns.d.ts | 1 - 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index 0261b983e74637..0ad23c533a9ede 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -710,6 +710,7 @@ mod tests { .compile_async(mock_state.clone(), &out, TargetLib::Main) .await; assert!(result.is_ok()); + eprintln!("asdf {}", result.as_ref().unwrap().code); assert!(result .unwrap() .code diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 8b4cbf32e11ea8..b5da4297bc7061 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -24,7 +24,6 @@ //! TODO: //! - rename DenoError to OpError? //! - rename JSError to RuntimeException. merge V8Exception? -//! - remove ErrorKind::WouldBlock //! - rename ErrorKind::Other. This corresponds to a generic exception thrown as the //! global `Error` in JS: //! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error @@ -59,7 +58,6 @@ pub enum ErrorKind { AddrNotAvailable = 8, BrokenPipe = 9, AlreadyExists = 10, - WouldBlock = 11, InvalidInput = 12, InvalidData = 13, TimedOut = 14, @@ -198,13 +196,13 @@ impl GetErrorKind for io::Error { AddrNotAvailable => ErrorKind::AddrNotAvailable, BrokenPipe => ErrorKind::BrokenPipe, AlreadyExists => ErrorKind::AlreadyExists, - WouldBlock => ErrorKind::WouldBlock, InvalidInput => ErrorKind::InvalidInput, InvalidData => ErrorKind::InvalidData, TimedOut => ErrorKind::TimedOut, Interrupted => ErrorKind::Interrupted, WriteZero => ErrorKind::WriteZero, UnexpectedEof => ErrorKind::UnexpectedEof, + WouldBlock => unreachable!(), _ => ErrorKind::Other, } } diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 587686b4414ace..03e126bfcaf9a4 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -48,7 +48,6 @@ export enum ErrorKind { AddrNotAvailable = 8, BrokenPipe = 9, AlreadyExists = 10, - WouldBlock = 11, InvalidInput = 12, InvalidData = 13, TimedOut = 14, diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 7edd4e535c661c..c03e078e079613 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1234,7 +1234,6 @@ declare namespace Deno { AddrNotAvailable = 8, BrokenPipe = 9, AlreadyExists = 10, - WouldBlock = 11, InvalidInput = 12, InvalidData = 13, TimedOut = 14, From 06f170cddcb7bbad13024f147f21f3e0e1b48433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 19 Feb 2020 17:59:04 -0500 Subject: [PATCH 15/26] remove debug statement --- cli/compilers/ts.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index 0ad23c533a9ede..0261b983e74637 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -710,7 +710,6 @@ mod tests { .compile_async(mock_state.clone(), &out, TargetLib::Main) .await; assert!(result.is_ok()); - eprintln!("asdf {}", result.as_ref().unwrap().code); assert!(result .unwrap() .code From 54674be7f8ed4554efe6eae2259c8b3770059df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 11:04:22 -0500 Subject: [PATCH 16/26] remove ErrorKind::UnixError --- cli/deno_error.rs | 111 ++++++++++++++++++++++++++++++++++++++-- cli/js/errors.ts | 2 - cli/js/event_target.ts | 1 + cli/js/lib.deno.ns.d.ts | 1 - 4 files changed, 109 insertions(+), 6 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index b5da4297bc7061..5777907060b8ad 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -67,7 +67,6 @@ pub enum ErrorKind { UnexpectedEof = 18, BadResource = 19, Http = 21, - UnixError = 24, URIError = 100, TypeError = 101, @@ -272,8 +271,114 @@ mod unix { Sys(EPERM) => ErrorKind::PermissionDenied, Sys(EINVAL) => ErrorKind::InvalidInput, Sys(ENOENT) => ErrorKind::NotFound, - Sys(_) => ErrorKind::UnixError, - _ => ErrorKind::Other, + Sys(UnknownErrno) => unreachable!(), + Sys(ESRCH) => unreachable!(), + Sys(EINTR) => unreachable!(), + Sys(EIO) => unreachable!(), + Sys(ENXIO) => unreachable!(), + Sys(E2BIG) => unreachable!(), + Sys(ENOEXEC) => unreachable!(), + Sys(EBADF) => unreachable!(), + Sys(ECHILD) => unreachable!(), + Sys(EDEADLK) => unreachable!(), + Sys(ENOMEM) => unreachable!(), + Sys(EACCES) => unreachable!(), + Sys(EFAULT) => unreachable!(), + Sys(ENOTBLK) => unreachable!(), + Sys(EBUSY) => unreachable!(), + Sys(EEXIST) => unreachable!(), + Sys(EXDEV) => unreachable!(), + Sys(ENODEV) => unreachable!(), + Sys(ENOTDIR) => unreachable!(), + Sys(EISDIR) => unreachable!(), + Sys(ENFILE) => unreachable!(), + Sys(EMFILE) => unreachable!(), + Sys(ENOTTY) => unreachable!(), + Sys(ETXTBSY) => unreachable!(), + Sys(EFBIG) => unreachable!(), + Sys(ENOSPC) => unreachable!(), + Sys(ESPIPE) => unreachable!(), + Sys(EROFS) => unreachable!(), + Sys(EMLINK) => unreachable!(), + Sys(EPIPE) => unreachable!(), + Sys(EDOM) => unreachable!(), + Sys(ERANGE) => unreachable!(), + Sys(EAGAIN) => unreachable!(), + Sys(EINPROGRESS) => unreachable!(), + Sys(EALREADY) => unreachable!(), + Sys(ENOTSOCK) => unreachable!(), + Sys(EDESTADDRREQ) => unreachable!(), + Sys(EMSGSIZE) => unreachable!(), + Sys(EPROTOTYPE) => unreachable!(), + Sys(ENOPROTOOPT) => unreachable!(), + Sys(EPROTONOSUPPORT) => unreachable!(), + Sys(ESOCKTNOSUPPORT) => unreachable!(), + Sys(ENOTSUP) => unreachable!(), + Sys(EPFNOSUPPORT) => unreachable!(), + Sys(EAFNOSUPPORT) => unreachable!(), + Sys(EADDRINUSE) => unreachable!(), + Sys(EADDRNOTAVAIL) => unreachable!(), + Sys(ENETDOWN) => unreachable!(), + Sys(ENETUNREACH) => unreachable!(), + Sys(ENETRESET) => unreachable!(), + Sys(ECONNABORTED) => unreachable!(), + Sys(ECONNRESET) => unreachable!(), + Sys(ENOBUFS) => unreachable!(), + Sys(EISCONN) => unreachable!(), + Sys(ENOTCONN) => unreachable!(), + Sys(ESHUTDOWN) => unreachable!(), + Sys(ETOOMANYREFS) => unreachable!(), + Sys(ETIMEDOUT) => unreachable!(), + Sys(ECONNREFUSED) => unreachable!(), + Sys(ELOOP) => unreachable!(), + Sys(ENAMETOOLONG) => unreachable!(), + Sys(EHOSTDOWN) => unreachable!(), + Sys(EHOSTUNREACH) => unreachable!(), + Sys(ENOTEMPTY) => unreachable!(), + Sys(EPROCLIM) => unreachable!(), + Sys(EUSERS) => unreachable!(), + Sys(EDQUOT) => unreachable!(), + Sys(ESTALE) => unreachable!(), + Sys(EREMOTE) => unreachable!(), + Sys(EBADRPC) => unreachable!(), + Sys(ERPCMISMATCH) => unreachable!(), + Sys(EPROGUNAVAIL) => unreachable!(), + Sys(EPROGMISMATCH) => unreachable!(), + Sys(EPROCUNAVAIL) => unreachable!(), + Sys(ENOLCK) => unreachable!(), + Sys(ENOSYS) => unreachable!(), + Sys(EFTYPE) => unreachable!(), + Sys(EAUTH) => unreachable!(), + Sys(ENEEDAUTH) => unreachable!(), + Sys(EPWROFF) => unreachable!(), + Sys(EDEVERR) => unreachable!(), + Sys(EOVERFLOW) => unreachable!(), + Sys(EBADEXEC) => unreachable!(), + Sys(EBADARCH) => unreachable!(), + Sys(ESHLIBVERS) => unreachable!(), + Sys(EBADMACHO) => unreachable!(), + Sys(ECANCELED) => unreachable!(), + Sys(EIDRM) => unreachable!(), + Sys(ENOMSG) => unreachable!(), + Sys(EILSEQ) => unreachable!(), + Sys(ENOATTR) => unreachable!(), + Sys(EBADMSG) => unreachable!(), + Sys(EMULTIHOP) => unreachable!(), + Sys(ENODATA) => unreachable!(), + Sys(ENOLINK) => unreachable!(), + Sys(ENOSR) => unreachable!(), + Sys(ENOSTR) => unreachable!(), + Sys(EPROTO) => unreachable!(), + Sys(ETIME) => unreachable!(), + Sys(EOPNOTSUPP) => unreachable!(), + Sys(ENOPOLICY) => unreachable!(), + Sys(ENOTRECOVERABLE) => unreachable!(), + Sys(EOWNERDEAD) => unreachable!(), + Sys(EQFULL) => unreachable!(), + Sys(_) => unreachable!(), + Error::InvalidPath => ErrorKind::TypeError, + Error::InvalidUtf8 => ErrorKind::InvalidData, + Error::UnsupportedOperation => unreachable!(), } } } diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 03e126bfcaf9a4..34157699a98f2b 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -57,8 +57,6 @@ export enum ErrorKind { UnexpectedEof = 18, BadResource = 19, Http = 21, - UnixError = 24, - TypeError = 101, UrlError = 100 } diff --git a/cli/js/event_target.ts b/cli/js/event_target.ts index 495c8a0429ebe2..caf5ab6f172241 100644 --- a/cli/js/event_target.ts +++ b/cli/js/event_target.ts @@ -418,6 +418,7 @@ const eventTargetHelpers = { try { listener.handleEvent(eventImpl); } catch (error) { + // TODO: remove this throw new DenoError(ErrorKind.Interrupted, error.message); } diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index c03e078e079613..3b45acecaca652 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1243,7 +1243,6 @@ declare namespace Deno { UnexpectedEof = 18, BadResource = 19, Http = 21, - UnixError = 24, TypeError = 101, UrlError = 100 } From 23fecc52940f3bf1ac5b544395d9dea80be9e0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 11:12:38 -0500 Subject: [PATCH 17/26] remove UnixError, InvalidInput --- cli/deno_error.rs | 16 +++++++--------- cli/js/dispatch_json_test.ts | 2 +- cli/js/dispatch_minimal_test.ts | 2 +- cli/js/errors.ts | 1 - cli/js/lib.deno.ns.d.ts | 1 - cli/js/process_test.ts | 3 +-- cli/js/text_encoding.ts | 6 ++---- cli/js/text_encoding_test.ts | 2 +- cli/ops/dispatch_minimal.rs | 2 +- 9 files changed, 14 insertions(+), 21 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 5777907060b8ad..f94cc46c27ef05 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -58,7 +58,6 @@ pub enum ErrorKind { AddrNotAvailable = 8, BrokenPipe = 9, AlreadyExists = 10, - InvalidInput = 12, InvalidData = 13, TimedOut = 14, Interrupted = 15, @@ -132,11 +131,11 @@ pub fn permission_denied_msg(msg: String) -> ErrBox { } pub fn no_buffer_specified() -> ErrBox { - StaticError(ErrorKind::InvalidInput, "no buffer specified").into() + StaticError(ErrorKind::TypeError, "no buffer specified").into() } pub fn invalid_address_syntax() -> ErrBox { - StaticError(ErrorKind::InvalidInput, "invalid address syntax").into() + StaticError(ErrorKind::TypeError, "invalid address syntax").into() } pub fn other_error(msg: String) -> ErrBox { @@ -195,7 +194,7 @@ impl GetErrorKind for io::Error { AddrNotAvailable => ErrorKind::AddrNotAvailable, BrokenPipe => ErrorKind::BrokenPipe, AlreadyExists => ErrorKind::AlreadyExists, - InvalidInput => ErrorKind::InvalidInput, + InvalidInput => ErrorKind::TypeError, InvalidData => ErrorKind::InvalidData, TimedOut => ErrorKind::TimedOut, Interrupted => ErrorKind::Interrupted, @@ -250,8 +249,8 @@ impl GetErrorKind for serde_json::error::Error { fn kind(&self) -> ErrorKind { use serde_json::error::*; match self.classify() { - Category::Io => ErrorKind::InvalidInput, - Category::Syntax => ErrorKind::InvalidInput, + Category::Io => ErrorKind::TypeError, + Category::Syntax => ErrorKind::TypeError, Category::Data => ErrorKind::InvalidData, Category::Eof => ErrorKind::UnexpectedEof, } @@ -269,7 +268,7 @@ mod unix { fn kind(&self) -> ErrorKind { match self { Sys(EPERM) => ErrorKind::PermissionDenied, - Sys(EINVAL) => ErrorKind::InvalidInput, + Sys(EINVAL) => ErrorKind::TypeError, Sys(ENOENT) => ErrorKind::NotFound, Sys(UnknownErrno) => unreachable!(), Sys(ESRCH) => unreachable!(), @@ -375,7 +374,6 @@ mod unix { Sys(ENOTRECOVERABLE) => unreachable!(), Sys(EOWNERDEAD) => unreachable!(), Sys(EQFULL) => unreachable!(), - Sys(_) => unreachable!(), Error::InvalidPath => ErrorKind::TypeError, Error::InvalidUtf8 => ErrorKind::InvalidData, Error::UnsupportedOperation => unreachable!(), @@ -511,7 +509,7 @@ mod tests { #[test] fn test_no_buffer_specified() { let err = no_buffer_specified(); - assert_eq!(err.kind(), ErrorKind::InvalidInput); + assert_eq!(err.kind(), ErrorKind::TypeError); assert_eq!(err.to_string(), "no buffer specified"); } } diff --git a/cli/js/dispatch_json_test.ts b/cli/js/dispatch_json_test.ts index c3fd6945b36568..b2e6e9371596e4 100644 --- a/cli/js/dispatch_json_test.ts +++ b/cli/js/dispatch_json_test.ts @@ -31,5 +31,5 @@ test(async function malformedJsonControlBuffer(): Promise { const resJson = JSON.parse(resText) as any; assert(!resJson.ok); assert(resJson.err); - assertEquals(resJson.err!.kind, Deno.ErrorKind.InvalidInput); + assertEquals(resJson.err!.kind, Deno.ErrorKind.TypeError); }); diff --git a/cli/js/dispatch_minimal_test.ts b/cli/js/dispatch_minimal_test.ts index 12c70d375b5aac..dcc48107b607a5 100644 --- a/cli/js/dispatch_minimal_test.ts +++ b/cli/js/dispatch_minimal_test.ts @@ -39,6 +39,6 @@ test(async function malformedMinimalControlBuffer(): Promise { const result = buf32[2]; const message = new TextDecoder().decode(res.slice(12)).trim(); assert(arg < 0); - assertEquals(result, Deno.ErrorKind.InvalidInput); + assertEquals(result, Deno.ErrorKind.TypeError); assertEquals(message, "Unparsable control buffer"); }); diff --git a/cli/js/errors.ts b/cli/js/errors.ts index 34157699a98f2b..fe79b60b69faf9 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -48,7 +48,6 @@ export enum ErrorKind { AddrNotAvailable = 8, BrokenPipe = 9, AlreadyExists = 10, - InvalidInput = 12, InvalidData = 13, TimedOut = 14, Interrupted = 15, diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 3b45acecaca652..3d8cfe104ebca5 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1234,7 +1234,6 @@ declare namespace Deno { AddrNotAvailable = 8, BrokenPipe = 9, AlreadyExists = 10, - InvalidInput = 12, InvalidData = 13, TimedOut = 14, Interrupted = 15, diff --git a/cli/js/process_test.ts b/cli/js/process_test.ts index dce4d991889945..c6ce1090060580 100644 --- a/cli/js/process_test.ts +++ b/cli/js/process_test.ts @@ -370,8 +370,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/text_encoding.ts b/cli/js/text_encoding.ts index ceb2f3fdc4e4e5..989071fe299561 100644 --- a/cli/js/text_encoding.ts +++ b/cli/js/text_encoding.ts @@ -105,8 +105,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, + throw new TypeError( "The string to be decoded is not correctly encoded" ); } @@ -130,8 +129,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 2422f86f6b7c84..28f23511a0c61a 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/ops/dispatch_minimal.rs b/cli/ops/dispatch_minimal.rs index 762146539f4f97..0bff571f2222ce 100644 --- a/cli/ops/dispatch_minimal.rs +++ b/cli/ops/dispatch_minimal.rs @@ -124,7 +124,7 @@ where let error_record = ErrorRecord { promise_id: 0, arg: -1, - error_code: ErrorKind::InvalidInput as i32, + error_code: ErrorKind::TypeError as i32, error_message: "Unparsable control buffer" .to_string() .as_bytes() From 45863e82f06eeefaecf0ccf869f0f866eb5fb28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 11:46:08 -0500 Subject: [PATCH 18/26] fmt --- cli/js/text_encoding.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cli/js/text_encoding.ts b/cli/js/text_encoding.ts index 989071fe299561..0e6882a56a99d2 100644 --- a/cli/js/text_encoding.ts +++ b/cli/js/text_encoding.ts @@ -105,9 +105,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 TypeError( - "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 From 2b4ee748a7b0f55d5c6346eaa69c567bd4b7d38c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 11:57:29 -0500 Subject: [PATCH 19/26] lint --- cli/js/text_encoding.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/js/text_encoding.ts b/cli/js/text_encoding.ts index 0e6882a56a99d2..0709e7123fba8c 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; From 20dfa2eb74c1bed11b9ca4f26187fb6cbd0eab65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 12:22:46 -0500 Subject: [PATCH 20/26] remove Deno.ErrorKind and Deno.Error --- cli/js/deno.ts | 2 +- cli/js/dir_test.ts | 4 +- cli/js/errors.ts | 210 +++++++++++++++--- cli/js/event_target.ts | 20 +- cli/js/lib.deno.ns.d.ts | 114 ++++++---- cli/js/os_test.ts | 2 +- cli/js/process_test.ts | 14 +- cli/tests/044_bad_resource.ts.out | 2 +- cli/tests/error_004_missing_module.ts.out | 2 +- .../error_005_missing_dynamic_import.ts.out | 2 +- cli/tests/error_006_import_ext_failure.ts.out | 2 +- std/encoding/yaml/error.ts | 6 +- std/fs/copy.ts | 4 +- std/fs/empty_dir.ts | 14 +- std/fs/ensure_dir.ts | 6 +- std/fs/ensure_file.ts | 6 +- std/fs/ensure_link_test.ts | 6 +- std/fs/exists.ts | 14 +- std/fs/expand_glob.ts | 6 +- std/fs/walk_test.ts | 9 +- std/http/file_server.ts | 4 +- std/node/process_test.ts | 4 +- std/testing/runner.ts | 4 +- 23 files changed, 298 insertions(+), 159 deletions(-) diff --git a/cli/js/deno.ts b/cli/js/deno.ts index cdfedcd76b3ed9..8ccca90961ce2b 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 4b03eaedd8260a..f936986aa271c2 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/errors.ts b/cli/js/errors.ts index fe79b60b69faf9..b00773e0e77d14 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -1,40 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -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.UrlError: - throw new URIError(msg); - default: - throw new DenoError(kind, msg); - } -} - -/** 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 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 { @@ -59,3 +24,178 @@ export enum ErrorKind { TypeError = 101, UrlError = 100 } + +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.UrlError: + 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, + PermissionDenied, + ConnectionRefused, + ConnectionReset, + ConnectionAborted, + NotConnected, + AddrInUse, + AddrNotAvailable, + BrokenPipe, + AlreadyExists, + InvalidData, + TimedOut, + Interrupted, + WriteZero, + Other, + UnexpectedEof, + BadResource, + Http +}; diff --git a/cli/js/event_target.ts b/cli/js/event_target.ts index caf5ab6f172241..daa73eb231b67b 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,8 +415,9 @@ const eventTargetHelpers = { try { listener.handleEvent(eventImpl); } catch (error) { - // TODO: remove this - 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/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 3d8cfe104ebca5..7b697d65fabf04 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1204,47 +1204,81 @@ 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 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, - InvalidData = 13, - TimedOut = 14, - Interrupted = 15, - WriteZero = 16, - Other = 17, - UnexpectedEof = 18, - BadResource = 19, - Http = 21, - TypeError = 101, - UrlError = 100 + 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); + } + + 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; + }; /** UNSTABLE: potentially want names to overlap more with browser. * diff --git a/cli/js/os_test.ts b/cli/js/os_test.ts index a461ba63ef4c07..3522a50f76dbbe 100644 --- a/cli/js/os_test.ts +++ b/cli/js/os_test.ts @@ -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` ); }); diff --git a/cli/js/process_test.ts b/cli/js/process_test.ts index c6ce1090060580..f55ce28fc9b894 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; @@ -78,8 +69,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( diff --git a/cli/tests/044_bad_resource.ts.out b/cli/tests/044_bad_resource.ts.out index f2a79476f0e2d8..92b493ca963eb7 100644 --- a/cli/tests/044_bad_resource.ts.out +++ b/cli/tests/044_bad_resource.ts.out @@ -1,7 +1,7 @@ [WILDCARD] error: Uncaught BadResource: bad resource id [WILDCARD]errors.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at BadResource ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_004_missing_module.ts.out b/cli/tests/error_004_missing_module.ts.out index 99bddb1d9057f5..492aef0e67321a 100644 --- a/cli/tests/error_004_missing_module.ts.out +++ b/cli/tests/error_004_missing_module.ts.out @@ -1,6 +1,6 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_004_missing_module.ts" [WILDCARD]errors.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at NotFound ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_005_missing_dynamic_import.ts.out b/cli/tests/error_005_missing_dynamic_import.ts.out index f08e2f102ca0cc..3b48e4e78a71a7 100644 --- a/cli/tests/error_005_missing_dynamic_import.ts.out +++ b/cli/tests/error_005_missing_dynamic_import.ts.out @@ -1,6 +1,6 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_005_missing_dynamic_import.ts" [WILDCARD]errors.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at NotFound ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/cli/tests/error_006_import_ext_failure.ts.out b/cli/tests/error_006_import_ext_failure.ts.out index ba6c7f20f6460c..e113bc26402f18 100644 --- a/cli/tests/error_006_import_ext_failure.ts.out +++ b/cli/tests/error_006_import_ext_failure.ts.out @@ -1,6 +1,6 @@ [WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/non-existent" from "[WILDCARD]/error_006_import_ext_failure.ts" [WILDCARD]errors.ts:[WILDCARD] - at DenoError ([WILDCARD]errors.ts:[WILDCARD]) + at NotFound ([WILDCARD]errors.ts:[WILDCARD]) at constructError ([WILDCARD]errors.ts:[WILDCARD]) at unwrapResponse ([WILDCARD]dispatch_json.ts:[WILDCARD]) at sendAsync[WILDCARD] ([WILDCARD]dispatch_json.ts:[WILDCARD]) diff --git a/std/encoding/yaml/error.ts b/std/encoding/yaml/error.ts index 62be8b477d2141..7f305ccf2c5d3a 100644 --- a/std/encoding/yaml/error.ts +++ b/std/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/std/fs/copy.ts b/std/fs/copy.ts index 86fac78dff7493..ec51784c664631 100644 --- a/std/fs/copy.ts +++ b/std/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/std/fs/empty_dir.ts b/std/fs/empty_dir.ts index e3d08ef70a35fc..0ac5e6420cfd5c 100644 --- a/std/fs/empty_dir.ts +++ b/std/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/std/fs/ensure_dir.ts b/std/fs/ensure_dir.ts index d4b30dd2d91196..b6cc3150a3566a 100644 --- a/std/fs/ensure_dir.ts +++ b/std/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/std/fs/ensure_file.ts b/std/fs/ensure_file.ts index 06c65b5f77e38a..be824b7bac1279 100644 --- a/std/fs/ensure_file.ts +++ b/std/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/std/fs/ensure_link_test.ts b/std/fs/ensure_link_test.ts index 6e9804152aa516..7549814a25d945 100644 --- a/std/fs/ensure_link_test.ts +++ b/std/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/std/fs/exists.ts b/std/fs/exists.ts index 4584dbff96f703..2cd4151739d5be 100644 --- a/std/fs/exists.ts +++ b/std/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/std/fs/expand_glob.ts b/std/fs/expand_glob.ts index aabf4a8a1b2f40..61fca96028c9ad 100644 --- a/std/fs/expand_glob.ts +++ b/std/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/std/fs/walk_test.ts b/std/fs/walk_test.ts index 4984546f21428f..f99f82eb516264 100644 --- a/std/fs/walk_test.ts +++ b/std/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/std/http/file_server.ts b/std/http/file_server.ts index aa0ff49daa0850..acc3a20cd20a37 100755 --- a/std/http/file_server.ts +++ b/std/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/std/node/process_test.ts b/std/node/process_test.ts index 51dbcd630b2281..545b6ce23f7505 100644 --- a/std/node/process_test.ts +++ b/std/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/std/testing/runner.ts b/std/testing/runner.ts index 8d6501e0295b8a..772e027241cf78 100755 --- a/std/testing/runner.ts +++ b/std/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); } From ca1b82afd49daaee819ba54f7cc11d6b3bc8e8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 15:46:03 -0500 Subject: [PATCH 21/26] use Deno.Err --- cli/js/chmod_test.ts | 10 +-- cli/js/chown_test.ts | 15 ++-- cli/js/copy_file_test.ts | 18 ++--- cli/js/dispatch_json_test.ts | 1 - cli/js/dispatch_minimal_test.ts | 1 - cli/js/errors.ts | 36 ++++----- cli/js/fetch_test.ts | 5 +- cli/js/files_test.ts | 7 +- cli/js/lib.deno.ns.d.ts | 130 ++++++++++++++------------------ cli/js/link_test.ts | 10 +-- cli/js/make_temp_test.ts | 16 ++-- cli/js/mkdir_test.ts | 5 +- cli/js/os_test.ts | 8 +- cli/js/process_test.ts | 6 +- cli/js/read_dir_test.ts | 8 +- cli/js/read_file_test.ts | 8 +- cli/js/read_link_test.ts | 8 +- cli/js/realpath_test.ts | 12 ++- cli/js/remove_test.ts | 64 ++++++---------- cli/js/rename_test.ts | 8 +- cli/js/stat_test.ts | 24 ++---- cli/js/symlink_test.ts | 2 +- cli/js/tls_test.ts | 12 ++- cli/js/truncate_test.ts | 6 +- cli/js/utime_test.ts | 10 +-- cli/js/write_file_test.ts | 14 ++-- 26 files changed, 181 insertions(+), 263 deletions(-) diff --git a/cli/js/chmod_test.ts b/cli/js/chmod_test.ts index b3b0a2ae2a6084..24c69db7ab7838 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 { } 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 { @@ -145,6 +143,6 @@ testPerm({ write: false }, async function chmodPerm(): Promise { } 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 61132ae31337e8..b4c1be3fd17ee9 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,7 +44,7 @@ if (Deno.build.os !== "win") { try { Deno.chownSync(filePath, uid, gid); } catch (e) { - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } } @@ -60,7 +59,7 @@ if (Deno.build.os !== "win") { try { await Deno.chown(filePath, uid, gid); } catch (e) { - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } } @@ -77,8 +76,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 +94,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 88dc3fdbf0226a..567b246b7babd9 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/dispatch_json_test.ts b/cli/js/dispatch_json_test.ts index b2e6e9371596e4..c714bb9ad440b6 100644 --- a/cli/js/dispatch_json_test.ts +++ b/cli/js/dispatch_json_test.ts @@ -31,5 +31,4 @@ test(async function malformedJsonControlBuffer(): Promise { const resJson = JSON.parse(resText) as any; assert(!resJson.ok); assert(resJson.err); - assertEquals(resJson.err!.kind, Deno.ErrorKind.TypeError); }); diff --git a/cli/js/dispatch_minimal_test.ts b/cli/js/dispatch_minimal_test.ts index dcc48107b607a5..b95a38d7db21a2 100644 --- a/cli/js/dispatch_minimal_test.ts +++ b/cli/js/dispatch_minimal_test.ts @@ -39,6 +39,5 @@ test(async function malformedMinimalControlBuffer(): Promise { const result = buf32[2]; const message = new TextDecoder().decode(res.slice(12)).trim(); assert(arg < 0); - assertEquals(result, Deno.ErrorKind.TypeError); assertEquals(message, "Unparsable control buffer"); }); diff --git a/cli/js/errors.ts b/cli/js/errors.ts index b00773e0e77d14..ab91591303f29d 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -180,22 +180,22 @@ class Http extends Error { } export const Err = { - NotFound, - PermissionDenied, - ConnectionRefused, - ConnectionReset, - ConnectionAborted, - NotConnected, - AddrInUse, - AddrNotAvailable, - BrokenPipe, - AlreadyExists, - InvalidData, - TimedOut, - Interrupted, - WriteZero, - Other, - UnexpectedEof, - BadResource, - Http + 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/fetch_test.ts b/cli/js/fetch_test.ts index 733ce878be7345..2912d5d2debe11 100644 --- a/cli/js/fetch_test.ts +++ b/cli/js/fetch_test.ts @@ -16,8 +16,7 @@ testPerm({ net: true }, async function fetchConnectionError(): Promise { } 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 { } catch (err_) { err = err_; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); diff --git a/cli/js/files_test.ts b/cli/js/files_test.ts index 398df06a7331c1..f71bfecc0b3414 100644 --- a/cli/js/files_test.ts +++ b/cli/js/files_test.ts @@ -79,7 +79,7 @@ testPerm({ write: false }, async function writePermFailure(): Promise { 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 { 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"); } } diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 7b697d65fabf04..4c1c309b9b8377 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1204,82 +1204,62 @@ declare namespace Deno { */ export function applySourceMap(location: Location): Location; - class NotFound extends Error { - constructor(msg: string); + 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); + } } - 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); - } - - 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; - }; - /** UNSTABLE: potentially want names to overlap more with browser. * * Permissions as granted by the caller diff --git a/cli/js/link_test.ts b/cli/js/link_test.ts index aada34b8488d49..22920713767c08 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 f6bf59d32dbbc7..06bfe0373895a0 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 { } 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 { } 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 dad61c1a471e7f..856cbdeb5b1f0c 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/os_test.ts b/cli/js/os_test.ts index 3522a50f76dbbe..fa4bf636b3a146 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"); }); @@ -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/process_test.ts b/cli/js/process_test.ts index f55ce28fc9b894..51ba8bfb3a6d26 100644 --- a/cli/js/process_test.ts +++ b/cli/js/process_test.ts @@ -14,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); }); @@ -322,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); }); diff --git a/cli/js/read_dir_test.ts b/cli/js/read_dir_test.ts index e3fa9b32d80037..4496d744756904 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); }); @@ -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 { 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 d40ea1b7b957f4..726f0841392eb6 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 { 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 c8db1cb5813e33..6f028c08d5025b 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 { 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 { 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 7443c78973940b..6dfec45fc810de 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 { 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 { 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 6b9e40b5ffe90e..cb89ea1a960283 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 { @@ -69,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( @@ -96,8 +93,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } } ); @@ -131,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); } } ); @@ -144,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"); }); @@ -163,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"; @@ -182,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( @@ -205,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); } ); @@ -219,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 { @@ -230,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"); }); @@ -253,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); } ); @@ -277,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); } ); @@ -309,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( @@ -336,8 +325,7 @@ testPerm( } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.NotFound); - assertEquals(err.name, "NotFound"); + assert(err instanceof Deno.Err.NotFound); } } ); @@ -371,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); } } ); @@ -384,7 +371,7 @@ testPerm({ write: false }, async function removePerm(): Promise { } catch (e) { err = e; } - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assert(err instanceof Deno.Err.PermissionDenied); assertEquals(err.name, "PermissionDenied"); }); @@ -405,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"; @@ -424,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); } ); @@ -448,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); } ); @@ -462,8 +447,7 @@ testPerm({ write: true }, async function removeAllFail(): Promise { } 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 { @@ -473,6 +457,6 @@ testPerm({ write: false }, async function removeAllPerm(): Promise { } 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 9c60e4d8cfa8b6..3673d848944649 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 bce5449acf664f..3914f877ca9ec4 100644 --- a/cli/js/stat_test.ts +++ b/cli/js/stat_test.ts @@ -23,8 +23,7 @@ testPerm({ read: false }, async function statSyncPerm(): Promise { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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/symlink_test.ts b/cli/js/symlink_test.ts index d5d2223ad6e692..b89b718b369fe1 100644 --- a/cli/js/symlink_test.ts +++ b/cli/js/symlink_test.ts @@ -31,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"); }); diff --git a/cli/js/tls_test.ts b/cli/js/tls_test.ts index 0f5abf1e9fdaef..dabbb2c899d303 100644 --- a/cli/js/tls_test.ts +++ b/cli/js/tls_test.ts @@ -13,7 +13,7 @@ test(async function connectTLSNoPerm(): Promise { } 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 { } 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 { } 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/truncate_test.ts b/cli/js/truncate_test.ts index c8809df9e5f110..42583354c2e82b 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 { } 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 72a4a647786974..bf1f7135380d21 100644 --- a/cli/js/utime_test.ts +++ b/cli/js/utime_test.ts @@ -78,7 +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); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } assert(caughtError); @@ -93,8 +93,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,7 +158,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); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } assert(caughtError); @@ -176,8 +175,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 2b952655f56b7f..a76d613af85f39 100644 --- a/cli/js/write_file_test.ts +++ b/cli/js/write_file_test.ts @@ -22,7 +22,7 @@ testPerm({ write: true }, function writeFileSyncFail(): void { Deno.writeFileSync(filename, data); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } assert(caughtError); @@ -38,8 +38,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,7 +65,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); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } assert(caughtError); @@ -128,7 +127,7 @@ testPerm( await Deno.writeFile(filename, data); } catch (e) { caughtError = true; - assertEquals(e.kind, Deno.ErrorKind.NotFound); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } assert(caughtError); @@ -147,8 +146,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,7 +178,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); + assert(e instanceof Deno.Err.NotFound); assertEquals(e.name, "NotFound"); } assert(caughtError); From 4871a0cfdf20cab2270dbd081d1ffe329ce70cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 15:53:42 -0500 Subject: [PATCH 22/26] lint --- cli/js/dispatch_json_test.ts | 1 - cli/js/dispatch_minimal_test.ts | 1 - cli/js/lib.deno.ns.d.ts | 5 ++++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cli/js/dispatch_json_test.ts b/cli/js/dispatch_json_test.ts index c714bb9ad440b6..90f93746ef5c99 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"; diff --git a/cli/js/dispatch_minimal_test.ts b/cli/js/dispatch_minimal_test.ts index b95a38d7db21a2..75381204f92bbe 100644 --- a/cli/js/dispatch_minimal_test.ts +++ b/cli/js/dispatch_minimal_test.ts @@ -36,7 +36,6 @@ test(async function malformedMinimalControlBuffer(): Promise { header.byteLength / 4 ); const arg = buf32[1]; - const result = buf32[2]; const message = new TextDecoder().decode(res.slice(12)).trim(); assert(arg < 0); assertEquals(message, "Unparsable control buffer"); diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 4c1c309b9b8377..651e7f562abb9c 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1203,7 +1203,8 @@ declare namespace Deno { * */ export function applySourceMap(location: Location): Location; - + + /* eslint-disable @typescript-eslint/no-unused-vars */ namespace Err { class NotFound extends Error { constructor(msg: string); @@ -1260,6 +1261,8 @@ declare namespace Deno { constructor(msg: string); } } + /* eslint-enable @typescript-eslint/no-unused-vars */ + /** UNSTABLE: potentially want names to overlap more with browser. * * Permissions as granted by the caller From 82e4a955cc1689a8b4bc2f7dc5d6a0c40e66cdde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 15:55:20 -0500 Subject: [PATCH 23/26] fmt --- cli/js/lib.deno.ns.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 651e7f562abb9c..fda2270a815148 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1203,7 +1203,7 @@ declare namespace Deno { * */ export function applySourceMap(location: Location): Location; - + /* eslint-disable @typescript-eslint/no-unused-vars */ namespace Err { class NotFound extends Error { From 1a7f22a7f4e8626f14b404a209d1018a3e827ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 16:09:14 -0500 Subject: [PATCH 24/26] fix tests --- cli/deno_error.rs | 104 +------------------------------------ cli/js/chown_test.ts | 2 - cli/js/dispatch_minimal.ts | 4 +- cli/js/net_test.ts | 9 ++-- cli/js/os.ts | 4 +- cli/js/streams/pipe-to.ts | 4 +- cli/js/utime_test.ts | 4 +- cli/js/write_file_test.ts | 4 -- 8 files changed, 11 insertions(+), 124 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index f94cc46c27ef05..9bc2c903ba82a9 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -271,109 +271,7 @@ mod unix { Sys(EINVAL) => ErrorKind::TypeError, Sys(ENOENT) => ErrorKind::NotFound, Sys(UnknownErrno) => unreachable!(), - Sys(ESRCH) => unreachable!(), - Sys(EINTR) => unreachable!(), - Sys(EIO) => unreachable!(), - Sys(ENXIO) => unreachable!(), - Sys(E2BIG) => unreachable!(), - Sys(ENOEXEC) => unreachable!(), - Sys(EBADF) => unreachable!(), - Sys(ECHILD) => unreachable!(), - Sys(EDEADLK) => unreachable!(), - Sys(ENOMEM) => unreachable!(), - Sys(EACCES) => unreachable!(), - Sys(EFAULT) => unreachable!(), - Sys(ENOTBLK) => unreachable!(), - Sys(EBUSY) => unreachable!(), - Sys(EEXIST) => unreachable!(), - Sys(EXDEV) => unreachable!(), - Sys(ENODEV) => unreachable!(), - Sys(ENOTDIR) => unreachable!(), - Sys(EISDIR) => unreachable!(), - Sys(ENFILE) => unreachable!(), - Sys(EMFILE) => unreachable!(), - Sys(ENOTTY) => unreachable!(), - Sys(ETXTBSY) => unreachable!(), - Sys(EFBIG) => unreachable!(), - Sys(ENOSPC) => unreachable!(), - Sys(ESPIPE) => unreachable!(), - Sys(EROFS) => unreachable!(), - Sys(EMLINK) => unreachable!(), - Sys(EPIPE) => unreachable!(), - Sys(EDOM) => unreachable!(), - Sys(ERANGE) => unreachable!(), - Sys(EAGAIN) => unreachable!(), - Sys(EINPROGRESS) => unreachable!(), - Sys(EALREADY) => unreachable!(), - Sys(ENOTSOCK) => unreachable!(), - Sys(EDESTADDRREQ) => unreachable!(), - Sys(EMSGSIZE) => unreachable!(), - Sys(EPROTOTYPE) => unreachable!(), - Sys(ENOPROTOOPT) => unreachable!(), - Sys(EPROTONOSUPPORT) => unreachable!(), - Sys(ESOCKTNOSUPPORT) => unreachable!(), - Sys(ENOTSUP) => unreachable!(), - Sys(EPFNOSUPPORT) => unreachable!(), - Sys(EAFNOSUPPORT) => unreachable!(), - Sys(EADDRINUSE) => unreachable!(), - Sys(EADDRNOTAVAIL) => unreachable!(), - Sys(ENETDOWN) => unreachable!(), - Sys(ENETUNREACH) => unreachable!(), - Sys(ENETRESET) => unreachable!(), - Sys(ECONNABORTED) => unreachable!(), - Sys(ECONNRESET) => unreachable!(), - Sys(ENOBUFS) => unreachable!(), - Sys(EISCONN) => unreachable!(), - Sys(ENOTCONN) => unreachable!(), - Sys(ESHUTDOWN) => unreachable!(), - Sys(ETOOMANYREFS) => unreachable!(), - Sys(ETIMEDOUT) => unreachable!(), - Sys(ECONNREFUSED) => unreachable!(), - Sys(ELOOP) => unreachable!(), - Sys(ENAMETOOLONG) => unreachable!(), - Sys(EHOSTDOWN) => unreachable!(), - Sys(EHOSTUNREACH) => unreachable!(), - Sys(ENOTEMPTY) => unreachable!(), - Sys(EPROCLIM) => unreachable!(), - Sys(EUSERS) => unreachable!(), - Sys(EDQUOT) => unreachable!(), - Sys(ESTALE) => unreachable!(), - Sys(EREMOTE) => unreachable!(), - Sys(EBADRPC) => unreachable!(), - Sys(ERPCMISMATCH) => unreachable!(), - Sys(EPROGUNAVAIL) => unreachable!(), - Sys(EPROGMISMATCH) => unreachable!(), - Sys(EPROCUNAVAIL) => unreachable!(), - Sys(ENOLCK) => unreachable!(), - Sys(ENOSYS) => unreachable!(), - Sys(EFTYPE) => unreachable!(), - Sys(EAUTH) => unreachable!(), - Sys(ENEEDAUTH) => unreachable!(), - Sys(EPWROFF) => unreachable!(), - Sys(EDEVERR) => unreachable!(), - Sys(EOVERFLOW) => unreachable!(), - Sys(EBADEXEC) => unreachable!(), - Sys(EBADARCH) => unreachable!(), - Sys(ESHLIBVERS) => unreachable!(), - Sys(EBADMACHO) => unreachable!(), - Sys(ECANCELED) => unreachable!(), - Sys(EIDRM) => unreachable!(), - Sys(ENOMSG) => unreachable!(), - Sys(EILSEQ) => unreachable!(), - Sys(ENOATTR) => unreachable!(), - Sys(EBADMSG) => unreachable!(), - Sys(EMULTIHOP) => unreachable!(), - Sys(ENODATA) => unreachable!(), - Sys(ENOLINK) => unreachable!(), - Sys(ENOSR) => unreachable!(), - Sys(ENOSTR) => unreachable!(), - Sys(EPROTO) => unreachable!(), - Sys(ETIME) => unreachable!(), - Sys(EOPNOTSUPP) => unreachable!(), - Sys(ENOPOLICY) => unreachable!(), - Sys(ENOTRECOVERABLE) => unreachable!(), - Sys(EOWNERDEAD) => unreachable!(), - Sys(EQFULL) => unreachable!(), + Sys(_) => unreachable!(), Error::InvalidPath => ErrorKind::TypeError, Error::InvalidUtf8 => ErrorKind::InvalidData, Error::UnsupportedOperation => unreachable!(), diff --git a/cli/js/chown_test.ts b/cli/js/chown_test.ts index b4c1be3fd17ee9..a1c12860d475b3 100644 --- a/cli/js/chown_test.ts +++ b/cli/js/chown_test.ts @@ -45,7 +45,6 @@ if (Deno.build.os !== "win") { Deno.chownSync(filePath, uid, gid); } catch (e) { assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } } ); @@ -60,7 +59,6 @@ if (Deno.build.os !== "win") { await Deno.chown(filePath, uid, gid); } catch (e) { assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } } ); diff --git a/cli/js/dispatch_minimal.ts b/cli/js/dispatch_minimal.ts index e5f62366a83a8e..203701085c2b81 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, constructError } from "./errors.ts"; +import { Err, ErrorKind, constructError } from "./errors.ts"; const promiseTableMin = new Map>(); // 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 { diff --git a/cli/js/net_test.ts b/cli/js/net_test.ts index f78f3a25d19c9a..a2f086f0a58a3c 100644 --- a/cli/js/net_test.ts +++ b/cli/js/net_test.ts @@ -169,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(); @@ -204,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(); @@ -231,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 554d4f78d723be..275dbdf1d599be 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/streams/pipe-to.ts b/cli/js/streams/pipe-to.ts index 3764e605b88e43..01608b955f920a 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> = []; // if (preventAbort === false) { // actions.push(() => { diff --git a/cli/js/utime_test.ts b/cli/js/utime_test.ts index bf1f7135380d21..c7e4293bf696a0 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). @@ -79,7 +79,6 @@ testPerm({ read: true, write: true }, function utimeSyncNotFound(): void { } catch (e) { caughtError = true; assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } assert(caughtError); }); @@ -159,7 +158,6 @@ testPerm({ read: true, write: true }, async function utimeNotFound(): Promise< } catch (e) { caughtError = true; assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } assert(caughtError); }); diff --git a/cli/js/write_file_test.ts b/cli/js/write_file_test.ts index a76d613af85f39..9f58f44602a9ef 100644 --- a/cli/js/write_file_test.ts +++ b/cli/js/write_file_test.ts @@ -23,7 +23,6 @@ testPerm({ write: true }, function writeFileSyncFail(): void { } catch (e) { caughtError = true; assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } assert(caughtError); }); @@ -66,7 +65,6 @@ testPerm({ read: true, write: true }, function writeFileSyncCreate(): void { } catch (e) { caughtError = true; assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } assert(caughtError); @@ -128,7 +126,6 @@ testPerm( } catch (e) { caughtError = true; assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } assert(caughtError); } @@ -179,7 +176,6 @@ testPerm({ read: true, write: true }, async function writeFileCreate(): Promise< } catch (e) { caughtError = true; assert(e instanceof Deno.Err.NotFound); - assertEquals(e.name, "NotFound"); } assert(caughtError); From d9fc8e360ac322fb074acbf51aeeee1fef9bab20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 16:16:26 -0500 Subject: [PATCH 25/26] order ErrorKind --- cli/deno_error.rs | 15 ++++++--------- cli/js/errors.ts | 14 +++++++------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 9bc2c903ba82a9..97c7c8a7c54a50 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -62,13 +62,12 @@ pub enum ErrorKind { TimedOut = 14, Interrupted = 15, WriteZero = 16, - Other = 17, - UnexpectedEof = 18, - BadResource = 19, - Http = 21, - - URIError = 100, - TypeError = 101, + UnexpectedEof = 17, + BadResource = 18, + Http = 19, + URIError = 20, + TypeError = 21, + Other = 22, } #[derive(Debug)] @@ -293,8 +292,6 @@ impl GetErrorKind for DlopenError { } } -// NOTE(bartlomieju): seems this is necessary - can't use ErrBox here -// TODO(bartlomieju): ultimately this should be rewritten to `RuntimeError`? impl GetErrorKind for dyn AnyError { fn kind(&self) -> ErrorKind { use self::GetErrorKind as Get; diff --git a/cli/js/errors.ts b/cli/js/errors.ts index ab91591303f29d..0844b0a569ca45 100644 --- a/cli/js/errors.ts +++ b/cli/js/errors.ts @@ -17,12 +17,12 @@ export enum ErrorKind { TimedOut = 14, Interrupted = 15, WriteZero = 16, - Other = 17, - UnexpectedEof = 18, - BadResource = 19, - Http = 21, - TypeError = 101, - UrlError = 100 + UnexpectedEof = 17, + BadResource = 18, + Http = 19, + URIError = 20, + TypeError = 21, + Other = 22 } export function constructError(kind: ErrorKind, msg: string): never { @@ -31,7 +31,7 @@ export function constructError(kind: ErrorKind, msg: string): never { throw new TypeError(msg); case ErrorKind.Other: throw new Error(msg); - case ErrorKind.UrlError: + case ErrorKind.URIError: throw new URIError(msg); case ErrorKind.NotFound: throw new NotFound(msg); From f7565121757b74523cbb2b0990c468ebea7f2132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 20 Feb 2020 16:37:50 -0500 Subject: [PATCH 26/26] fix std tests --- std/node/module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/node/module.ts b/std/node/module.ts index 547c76bab17992..aecf03ede6f4ed 100644 --- a/std/node/module.ts +++ b/std/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;