-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib,crypto: add optional cause to DOMException
#44224
Conversation
Review requested:
|
fe5da63
to
ed6e2a7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe API changes to web interfaces should be made to their standards first: https://webidl.spec.whatwg.org/#idl-DOMException. From what I can tell, there are discussions on overloading the second parameter of DOMException to accept an option bag instead of introducing a third positional parameter for cause: whatwg/webidl#969
This can be a good addition! I'm going to submit a PR on WebIDL to define the API changes on DOMException: whatwg/webidl#1179. |
Given DOMExceptions are instances of Error I assumed this would be a fair game. Probably makes sense to resolve idl if this changes the exposed constructor tho. Question: If I change the PR to not modify the constructor but still have the cause property, would that be ok now? |
cause | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that be more correct? (at least that's the 'cause' in new Error === false
, and 'cause' in new Error('', { cause: undefined})) === true
)
cause | |
}); | |
}); | |
if (arguments.length > 2) { | |
ObjectDefineProperty(this, 'cause', { | |
__proto__: null, | |
configurable: true, | |
enumerable: true, | |
value: cause, | |
writable: true, | |
}); | |
} |
@@ -49,14 +49,23 @@ const disusedNamesSet = new SafeSet() | |||
.add('ValidationError'); | |||
|
|||
class DOMException { | |||
constructor(message = '', name = 'Error') { | |||
constructor(message = '', name = 'Error', cause = undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this signature preferable over any of these?
constructor(message = '', name = 'Error', cause = undefined) { | |
constructor(message = '', name = 'Error', options = { cause }) { // pseudocode |
or
constructor(message = '', name = 'Error', cause = undefined) { | |
constructor(message = '', nameOrOptions = 'Error') { |
I'm unsure how the property The use case with undefined cause for DOMException might be negligible, as the DOMException is mostly constructed by the host environment. It's probably fine for DOMException to always define the |
}); | ||
} | ||
|
||
get cause() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the debate (whatwg/webidl#1179) that is happening in the PR that is updating DOMException
to have a cause
there is a disagreement on whether cause
should be an own property or an accessor. For all intrinsic Error
s, TC-39 decided that cause
should be an own data property in order to distinguish between the case where cause
is explicitly not given vs. cause
is explicitly undefined
const err1 = new Error('no cause');
console.log('cause' in err1); // false
console.log(err1.cause); // undefined
const err2 = new Error('undefined cause', { cause: undefined });
console.log('cause' in err2); // true
console.log(err2.cause); // undefined
From all appearances, it seems a decision is going to be made that DOMException
should be different that intrinsic errors and will use an accessor instead, making it impossible to distinguish between these cases.
const err1 = new DOMException('no cause', 'ABORT_ERR');
console.log('cause' in err1); // true
console.log(err1.cause); // undefined
const err2 = new DOMException('undefined cause', { name: 'ABORT_ERR', cause: undefined });
console.log('cause' in err2); // true
console.log(err2.cause); // undefined
The reasons for diverging do not seem that well defined beyond consistency with the way name
and message
are currently handled for DOMException
but it's not really a compelling reason for me.
For our implementation, I'd rather we choose to align with TC-39's definition and intentionally diverge from the official definition that is likely to land in the WebIDL spec.
tl;dr ... let's not use an accessor for cause
. Let's make it an own data property on the DOMException
instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell I don't want to close this PR just yet given there are ongoing discussions, but when the dust settles please open a new PR with whatwg/webidl#1179 that will supersede this one. |
@panva ... absolutely. I've asked my son @flakey5 if he'd like to take an a variation of this that follows the approach in @legendecas' proposal but using the own property instead of the getter. He's working on the new PR now. |
New alternative PR: #44703 |
Adds an optional
cause
toDOMException
, put to use for operation specific WebCryptoAPI errors.