Skip to content

Commit

Permalink
feat: support options in error constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jan 10, 2025
1 parent fb8ce24 commit 702074c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/01_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,24 +301,24 @@
// errors in the JS code (eg. "deno_net") so they are provided in "Deno.core"
// but later reexported on "Deno.errors"
class BadResource extends Error {
constructor(msg) {
super(msg);
constructor(msg, options) {
super(msg, options);
this.name = "BadResource";
}
}
const BadResourcePrototype = BadResource.prototype;

class Interrupted extends Error {
constructor(msg) {
super(msg);
constructor(msg, options) {
super(msg, options);
this.name = "Interrupted";
}
}
const InterruptedPrototype = Interrupted.prototype;

class NotCapable extends Error {
constructor(msg) {
super(msg);
constructor(msg, options) {
super(msg, options);
this.name = "NotCapable";
}
}
Expand Down
15 changes: 15 additions & 0 deletions testing/unit/error_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@ test(function testCustomError() {
assert(e instanceof Deno.core.BadResource);
}
});

test(function testJsErrorConstructors() {
const error = new Error("message");
const badResource = new Deno.core.BadResource("bad resource", { cause: error });
assertEquals(badResource.message, "bad resource");
assertEquals(badResource.cause, error);

const Interrupted = new Deno.core.Interrupted("interrupted", { cause: error });
assertEquals(Interrupted.message, "interrupted");
assertEquals(Interrupted.cause, error);

const notCapable = new Deno.core.NotCapable("not capable", { cause: error });
assertEquals(notCapable.message, "not capable");
assertEquals(notCapable.cause, error);
})

0 comments on commit 702074c

Please sign in to comment.