Skip to content

Commit 2d6ae00

Browse files
achim-kjtbandes
andauthored
include cause property in communicated error fields (#17)
Prior to this PR, the `cause` field of thrown errors was not passed across the comlink link. This PR adds support for this --------- Co-authored-by: Jacob Bandes-Storch <jacob@bandes-stor.ch>
1 parent 082b625 commit 2d6ae00

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/comlink.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ const throwTransferHandler: TransferHandler<
256256
message: value.message,
257257
name: value.name,
258258
stack: value.stack,
259+
cause: value.cause,
259260
},
260261
};
261262
} else {
@@ -266,7 +267,7 @@ const throwTransferHandler: TransferHandler<
266267
deserialize(serialized) {
267268
if (serialized.isError) {
268269
throw Object.assign(
269-
new Error(serialized.value.message),
270+
new Error(serialized.value.message, { cause: serialized.value.cause }),
270271
serialized.value
271272
);
272273
}

tests/same_window.comlink.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ describe("Comlink in the same realm", function () {
9898
expect(await thing.x).to.be.undefined;
9999
});
100100

101-
it("can keep the stack and message of thrown errors", async function () {
101+
it("can keep the stack, message and cause of thrown errors", async function () {
102102
let stack;
103103
const thing = Comlink.wrap(this.port1);
104104
Comlink.expose((_) => {
105-
const error = Error("OMG");
105+
const error = Error("OMG", { cause: "the cause" });
106106
stack = error.stack;
107107
throw error;
108108
}, this.port2);
@@ -113,6 +113,7 @@ describe("Comlink in the same realm", function () {
113113
expect(err).to.not.eq("Should have thrown");
114114
expect(err.message).to.equal("OMG");
115115
expect(err.stack).to.equal(stack);
116+
expect(err.cause).to.equal("the cause");
116117
}
117118
});
118119

0 commit comments

Comments
 (0)