Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/internal/per_context/domexception.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

const {
ErrorCaptureStackTrace,
Error,
ErrorPrototype,
FunctionPrototype,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectConstruct,
SafeMap,
SafeSet,
SafeWeakMap,
Expand Down Expand Up @@ -48,19 +50,27 @@
.add('NoDataAllowedError')
.add('ValidationError');

class DOMException {
// The DOMException class must not inherit from the Error class,
// but its instance inherit from Error.prototype.
// Make the runtime aware not to allocate `this` instance unnecessarily.
class DOMException extends Error {
static {
ObjectSetPrototypeOf(DOMException, FunctionPrototype);
}

constructor(message = '', options = 'Error') {
ErrorCaptureStackTrace(this);
// The stack property and [[ErrorData]] internal slot are given.
const error = ReflectConstruct(Error, [], new.target);

if (options && typeof options === 'object') {
const { name } = options;
internalsMap.set(this, {
internalsMap.set(error, {
message: `${message}`,
name: `${name}`,
});

if ('cause' in options) {
ObjectDefineProperty(this, 'cause', {
ObjectDefineProperty(error, 'cause', {
__proto__: null,
value: options.cause,
configurable: true,
Expand All @@ -69,11 +79,13 @@
});
}
} else {
internalsMap.set(this, {
internalsMap.set(error, {
message: `${message}`,
name: `${options}`,
});
}

return error;

Check failure on line 88 in lib/internal/per_context/domexception.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected return statement in constructor
}

get name() {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
assert.strictEqual(util.types.isNativeError(new Error()), true);
assert.strictEqual(util.types.isNativeError(new TypeError()), true);
assert.strictEqual(util.types.isNativeError(new SyntaxError()), true);
assert.strictEqual(util.types.isNativeError(new DOMException()), true);
assert.strictEqual(util.types.isNativeError(new (context('Error'))()), true);
assert.strictEqual(
util.types.isNativeError(new (context('TypeError'))()),
Expand Down
Loading