Skip to content

Commit ab28941

Browse files
committed
errors: make AbortError structured cloneable
Refs: #41038 Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 65ecdd4 commit ab28941

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/internal/errors.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ const {
6060
URIError,
6161
} = primordials;
6262

63+
const {
64+
makeTransferable,
65+
kClone,
66+
kDeserialize,
67+
} = require('internal/worker/js_transferable');
68+
6369
const kIsNodeError = Symbol('kIsNodeError');
6470

6571
const isWindows = process.platform === 'win32';
@@ -828,6 +834,29 @@ class AbortError extends Error {
828834
super(message, options);
829835
this.code = 'ABORT_ERR';
830836
this.name = 'AbortError';
837+
// eslint-disable-next-line no-constructor-return
838+
return makeTransferable(this);
839+
}
840+
841+
[kClone]() {
842+
const name = this.name;
843+
const message = this.message;
844+
const stack = this.stack;
845+
const code = this.code;
846+
const cause = this.cause;
847+
848+
return {
849+
data: { name, message, stack, code, cause },
850+
deserializeInfo: 'internal/errors:AbortError',
851+
};
852+
}
853+
854+
[kDeserialize]({ name, message, stack, code, cause }) {
855+
this.name = name;
856+
this.message = message;
857+
this.stack = stack;
858+
this.code = code;
859+
this.cause = cause;
831860
}
832861
}
833862
module.exports = {

test/parallel/test-errors-cloneabledomexception.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
// Flags: --expose-internals
12
'use strict';
23

34
const common = require('../common');
45

5-
const { strictEqual, ok } = require('assert');
6+
const {
7+
strictEqual,
8+
notStrictEqual,
9+
ok,
10+
} = require('assert');
11+
const { AbortError } = require('internal/errors');
612

713
const exception = new DOMException('foo', 'AbortError');
814
strictEqual(exception.name, 'AbortError');
@@ -19,3 +25,17 @@ mc.port1.onmessage = common.mustCall(({ data }) => {
1925
mc.port1.close();
2026
});
2127
mc.port2.postMessage(exception);
28+
29+
// Let's make sure AbortError is cloneable also
30+
const abort = new AbortError();
31+
const mc2 = new MessageChannel();
32+
mc2.port1.onmessage = common.mustCall(({ data }) => {
33+
ok(data instanceof AbortError);
34+
ok(data instanceof Error);
35+
notStrictEqual(data, abort);
36+
strictEqual(data.name, abort.name);
37+
strictEqual(data.message, abort.message);
38+
strictEqual(data.code, abort.code);
39+
mc2.port1.close();
40+
});
41+
mc2.port2.postMessage(abort);

0 commit comments

Comments
 (0)