Skip to content

Commit c33cb58

Browse files
author
A-yon Lee
committed
add tests
1 parent 3e16291 commit c33cb58

4 files changed

Lines changed: 60 additions & 8 deletions

File tree

error.test.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { isDeno } from "./env.ts";
44
import { pick } from "./object.ts";
55
import { toFsPath } from "./path.ts";
66
import { createErrorEvent } from "./event.ts";
7+
import { isSubclassOf } from "./class.ts";
8+
import * as common from "./error/common.ts";
9+
import { getErrorConstructor, registerErrorType } from "./error.ts";
710

811
declare var AggregateError: new (errors: Error[], message?: string, options?: { cause: unknown; }) => Error & { errors: Error[]; };
912

@@ -315,7 +318,7 @@ describe("Error", () => {
315318
strictEqual(normalize(event.filename), toFsPath(import.meta.url));
316319

317320
if (isDeno) {
318-
strictEqual(event.lineno, 300);
321+
strictEqual(event.lineno, 303);
319322
strictEqual(event.colno, 21);
320323
} else {
321324
ok(event.lineno > 0);
@@ -335,7 +338,7 @@ describe("Error", () => {
335338
strictEqual(normalize(event2.filename), toFsPath(import.meta.url));
336339

337340
if (isDeno) {
338-
strictEqual(event2.lineno, 325);
341+
strictEqual(event2.lineno, 328);
339342
strictEqual(event2.colno, 22);
340343
} else {
341344
ok(event2.lineno > 0);
@@ -460,4 +463,53 @@ describe("Error", () => {
460463
ok(!err2.isCausedBy(Error3));
461464
});
462465
});
466+
467+
describe("registerErrorType", () => {
468+
it("register and retrieve", () => {
469+
class MyError extends Exception {
470+
constructor(message: string, options: ErrorOptions = {}) {
471+
super(message, { ...options, name: "MyError" });
472+
}
473+
}
474+
475+
registerErrorType(MyError);
476+
strictEqual(getErrorConstructor("MyError"), MyError);
477+
478+
const obj = Error.toObject(new MyError("something went wrong", {
479+
cause: "unknown",
480+
}));
481+
const err = Error.fromObject(obj);
482+
strictEqual(err?.constructor, MyError);
483+
strictEqual(err?.name, obj["name"]);
484+
strictEqual(err?.message, obj["message"]);
485+
strictEqual(err?.stack, obj["stack"]);
486+
// @ts-ignore
487+
strictEqual(err?.cause, obj["cause"]);
488+
// @ts-ignore
489+
strictEqual(err?.code, obj["code"]);
490+
});
491+
492+
it("common error types", () => {
493+
ok(Object.values(common).some(ctor => isSubclassOf(ctor, Exception)));
494+
495+
for (const ctor of Object.values(common)) {
496+
if (!isSubclassOf(ctor, Exception)) {
497+
continue;
498+
}
499+
500+
const obj = Error.toObject(new ctor("something went wrong", {
501+
cause: "unknown",
502+
}));
503+
const err = Error.fromObject(obj);
504+
strictEqual(err?.constructor, ctor);
505+
strictEqual(err?.name, obj["name"]);
506+
strictEqual(err?.message, obj["message"]);
507+
strictEqual(err?.stack, obj["stack"]);
508+
// @ts-ignore
509+
strictEqual(err?.cause, obj["cause"]);
510+
// @ts-ignore
511+
strictEqual(err?.code, obj["code"]);
512+
}
513+
});
514+
});
463515
});

error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function registerErrorType<T extends Error>(ctor: Constructor<T>): void {
3333
* Returns the error constructor by the `name`.
3434
* @inner
3535
*/
36-
export function getErrorType<T extends Error>(name: string): Constructor<T> | null {
36+
export function getErrorConstructor<T extends Error>(name: string): Constructor<T> | null {
3737
let type = errorTypeRegistry.get(name);
3838

3939
if (!type && name in globalThis) {
@@ -133,7 +133,7 @@ export function fromObject<T extends Error>(
133133
// @ts-ignore
134134
const typeName: string = obj["@@type"] || obj.name;
135135
// @ts-ignore
136-
ctor ??= (getErrorType(typeName) ?? Error) as Constructor<T>;
136+
ctor ??= (getErrorConstructor(typeName) ?? Error) as Constructor<T>;
137137
let err: T;
138138

139139
if (ctor.name === "DOMException" && typeof DOMException === "function") {

parallel/threads.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
fromObject,
1414
isAggregateError,
1515
isDOMException,
16-
getErrorType,
16+
getErrorConstructor,
1717
toObject,
1818
} from "../error.ts";
1919
import * as path from "../path.ts";
@@ -511,7 +511,7 @@ export function wrapArgs<A extends any[]>(
511511
export function unwrapReturnValue(value: any): any {
512512
if (isPlainObject(value) &&
513513
typeof value["@@type"] === "string" &&
514-
getErrorType(value["@@type"])
514+
getErrorConstructor(value["@@type"])
515515
) {
516516
return fromObject(value);
517517
}

parallel/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isPlainObject } from "../object.ts";
66
import {
77
Exception,
88
fromObject,
9-
getErrorType,
9+
getErrorConstructor,
1010
isAggregateError,
1111
isDOMException,
1212
toObject,
@@ -52,7 +52,7 @@ function unwrapArgs(
5252
if (isPlainObject(arg)) {
5353
if (arg["@@type"] === "Channel" && typeof arg["@@id"] === "number") {
5454
return unwrapChannel(arg as any, channelWrite);
55-
} else if (typeof arg["@@type"] === "string" && getErrorType(arg["@@type"])) {
55+
} else if (typeof arg["@@type"] === "string" && getErrorConstructor(arg["@@type"])) {
5656
return fromObject(arg);
5757
}
5858
}

0 commit comments

Comments
 (0)