summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNayeem Rahman <muhammed.9939@gmail.com>2020-02-29 18:04:10 +0000
committerGitHub <noreply@github.com>2020-02-29 19:04:10 +0100
commit1f9d158bdc212e8cb15b92039c9cd11884c9708c (patch)
tree4c89fc3bb84c793f18ba8c5509ebf4dd1f6781ba
parentf55b22e195ff0dfaf117aaef2a0fcc51fe0058c8 (diff)
refactor(cli/js): Replace constructError() with getErrorClass() (#4189)
Flattens dispatch error handling to produce one less useless stack frame on op errors.
-rw-r--r--cli/js/dispatch_json.ts4
-rw-r--r--cli/js/dispatch_minimal.ts4
-rw-r--r--cli/js/errors.ts42
-rw-r--r--cli/tests/044_bad_resource.ts.out3
-rw-r--r--cli/tests/error_004_missing_module.ts.out3
-rw-r--r--cli/tests/error_005_missing_dynamic_import.ts.out3
-rw-r--r--cli/tests/error_006_import_ext_failure.ts.out3
-rw-r--r--cli/tests/error_011_bad_module_specifier.ts.out3
-rw-r--r--cli/tests/error_012_bad_dynamic_import_specifier.ts.out3
-rw-r--r--cli/tests/error_type_definitions.ts.out3
10 files changed, 32 insertions, 39 deletions
diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts
index 12c2d8821..63789044c 100644
--- a/cli/js/dispatch_json.ts
+++ b/cli/js/dispatch_json.ts
@@ -3,7 +3,7 @@ import * as util from "./util.ts";
import { TextEncoder, TextDecoder } from "./text_encoding.ts";
import { core } from "./core.ts";
import { OPS_CACHE } from "./runtime.ts";
-import { ErrorKind, constructError } from "./errors.ts";
+import { ErrorKind, getErrorClass } from "./errors.ts";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Ok = any;
@@ -38,7 +38,7 @@ function encode(args: object): Uint8Array {
function unwrapResponse(res: JsonResponse): Ok {
if (res.err != null) {
- return constructError(res.err!.kind, res.err!.message);
+ throw new (getErrorClass(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 988cb8af2..5778b1333 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 { errors, ErrorKind, constructError } from "./errors.ts";
+import { ErrorKind, errors, getErrorClass } from "./errors.ts";
const promiseTableMin = new Map<number, util.Resolvable<RecordMinimal>>();
// Note it's important that promiseId starts at 1 instead of 0, because sync
@@ -56,7 +56,7 @@ export function recordFromBufMinimal(ui8: Uint8Array): RecordMinimal {
function unwrapResponse(res: RecordMinimal): number {
if (res.err != null) {
- return constructError(res.err!.kind, res.err!.message);
+ throw new (getErrorClass(res.err.kind))(res.err.message);
}
return res.result;
}
diff --git a/cli/js/errors.ts b/cli/js/errors.ts
index 0379f3c96..44f35b438 100644
--- a/cli/js/errors.ts
+++ b/cli/js/errors.ts
@@ -25,48 +25,48 @@ export enum ErrorKind {
Other = 22
}
-export function constructError(kind: ErrorKind, msg: string): never {
+export function getErrorClass(kind: ErrorKind): { new (msg: string): Error } {
switch (kind) {
case ErrorKind.TypeError:
- throw new TypeError(msg);
+ return TypeError;
case ErrorKind.Other:
- throw new Error(msg);
+ return Error;
case ErrorKind.URIError:
- throw new URIError(msg);
+ return URIError;
case ErrorKind.NotFound:
- throw new NotFound(msg);
+ return NotFound;
case ErrorKind.PermissionDenied:
- throw new PermissionDenied(msg);
+ return PermissionDenied;
case ErrorKind.ConnectionRefused:
- throw new ConnectionRefused(msg);
+ return ConnectionRefused;
case ErrorKind.ConnectionReset:
- throw new ConnectionReset(msg);
+ return ConnectionReset;
case ErrorKind.ConnectionAborted:
- throw new ConnectionAborted(msg);
+ return ConnectionAborted;
case ErrorKind.NotConnected:
- throw new NotConnected(msg);
+ return NotConnected;
case ErrorKind.AddrInUse:
- throw new AddrInUse(msg);
+ return AddrInUse;
case ErrorKind.AddrNotAvailable:
- throw new AddrNotAvailable(msg);
+ return AddrNotAvailable;
case ErrorKind.BrokenPipe:
- throw new BrokenPipe(msg);
+ return BrokenPipe;
case ErrorKind.AlreadyExists:
- throw new AlreadyExists(msg);
+ return AlreadyExists;
case ErrorKind.InvalidData:
- throw new InvalidData(msg);
+ return InvalidData;
case ErrorKind.TimedOut:
- throw new TimedOut(msg);
+ return TimedOut;
case ErrorKind.Interrupted:
- throw new Interrupted(msg);
+ return Interrupted;
case ErrorKind.WriteZero:
- throw new WriteZero(msg);
+ return WriteZero;
case ErrorKind.UnexpectedEof:
- throw new UnexpectedEof(msg);
+ return UnexpectedEof;
case ErrorKind.BadResource:
- throw new BadResource(msg);
+ return BadResource;
case ErrorKind.Http:
- throw new Http(msg);
+ return Http;
}
}
diff --git a/cli/tests/044_bad_resource.ts.out b/cli/tests/044_bad_resource.ts.out
index 92b493ca9..681bed999 100644
--- a/cli/tests/044_bad_resource.ts.out
+++ b/cli/tests/044_bad_resource.ts.out
@@ -1,7 +1,6 @@
[WILDCARD]
error: Uncaught BadResource: bad resource id
-[WILDCARD]errors.ts:[WILDCARD]
+[WILDCARD]dispatch_json.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 492aef0e6..8e9f7f925 100644
--- a/cli/tests/error_004_missing_module.ts.out
+++ b/cli/tests/error_004_missing_module.ts.out
@@ -1,6 +1,5 @@
[WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_004_missing_module.ts"
-[WILDCARD]errors.ts:[WILDCARD]
+[WILDCARD]dispatch_json.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 3b48e4e78..7eb20fe04 100644
--- a/cli/tests/error_005_missing_dynamic_import.ts.out
+++ b/cli/tests/error_005_missing_dynamic_import.ts.out
@@ -1,6 +1,5 @@
[WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts" from "[WILDCARD]/error_005_missing_dynamic_import.ts"
-[WILDCARD]errors.ts:[WILDCARD]
+[WILDCARD]dispatch_json.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 e113bc264..41b9b7bf8 100644
--- a/cli/tests/error_006_import_ext_failure.ts.out
+++ b/cli/tests/error_006_import_ext_failure.ts.out
@@ -1,6 +1,5 @@
[WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/non-existent" from "[WILDCARD]/error_006_import_ext_failure.ts"
-[WILDCARD]errors.ts:[WILDCARD]
+[WILDCARD]dispatch_json.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_011_bad_module_specifier.ts.out b/cli/tests/error_011_bad_module_specifier.ts.out
index 7bf7ef26a..054f05fd5 100644
--- a/cli/tests/error_011_bad_module_specifier.ts.out
+++ b/cli/tests/error_011_bad_module_specifier.ts.out
@@ -1,6 +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]errors.ts:[WILDCARD]
- at constructError ($deno$/errors.ts:[WILDCARD])
+[WILDCARD]dispatch_json.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 9f41f9b28..1ac8f855f 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,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]errors.ts:[WILDCARD]
- at constructError ($deno$/errors.ts:[WILDCARD])
+[WILDCARD]dispatch_json.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 04b012ead..90a3211e9 100644
--- a/cli/tests/error_type_definitions.ts.out
+++ b/cli/tests/error_type_definitions.ts.out
@@ -1,6 +1,5 @@
[WILDCARD]error: Uncaught URIError: relative import path "baz" not prefixed with / or ./ or ../ Imported from "[WILDCARD]/type_definitions/bar.d.ts"
-[WILDCARD]errors.ts:[WILDCARD]
- at constructError ($deno$/errors.ts:[WILDCARD])
+[WILDCARD]dispatch_json.ts:[WILDCARD]
at unwrapResponse ($deno$/dispatch_json.ts:[WILDCARD])
at sendSync ($deno$/dispatch_json.ts:[WILDCARD])
at resolveModules ($deno$/compiler_imports.ts:[WILDCARD])