Skip to content

Commit

Permalink
errors: make AbortError structured cloneable
Browse files Browse the repository at this point in the history
Refs: #41038
Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed Dec 1, 2021
1 parent db91e84 commit afdcc0e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
29 changes: 29 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const {
URIError,
} = primordials;

const {
makeTransferable,
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');

const kIsNodeError = Symbol('kIsNodeError');

const isWindows = process.platform === 'win32';
Expand Down Expand Up @@ -825,6 +831,29 @@ class AbortError extends Error {
super('The operation was aborted');
this.code = 'ABORT_ERR';
this.name = 'AbortError';
// eslint-disable-next-line no-constructor-return
return makeTransferable(this);
}

[kClone]() {
const name = this.name;
const message = this.message;
const stack = this.stack;
const code = this.code;
const cause = this.cause;

return {
data: { name, message, stack, code, cause },
deserializeInfo: 'internal/errors:AbortError',
};
}

[kDeserialize]({ name, message, stack, code, cause }) {
this.name = name;
this.message = message;
this.stack = stack;
this.code = code;
this.cause = cause;
}
}
module.exports = {
Expand Down
22 changes: 21 additions & 1 deletion test/parallel/test-errors-cloneabledomexception.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// Flags: --expose-internals
'use strict';

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

const { strictEqual, ok } = require('assert');
const {
strictEqual,
notStrictEqual,
ok,
} = require('assert');
const { AbortError } = require('internal/errors');

const exception = new DOMException('foo', 'AbortError');
strictEqual(exception.name, 'AbortError');
Expand All @@ -19,3 +25,17 @@ mc.port1.onmessage = common.mustCall(({ data }) => {
mc.port1.close();
});
mc.port2.postMessage(exception);

// Let's make sure AbortError is cloneable also
const abort = new AbortError();
const mc2 = new MessageChannel();
mc2.port1.onmessage = common.mustCall(({ data }) => {
ok(data instanceof AbortError);
ok(data instanceof Error);
notStrictEqual(data, abort);
strictEqual(data.name, abort.name);
strictEqual(data.message, abort.message);
strictEqual(data.code, abort.code);
mc2.port1.close();
});
mc2.port2.postMessage(abort);

0 comments on commit afdcc0e

Please sign in to comment.