Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

panva
Copy link
Member

@panva panva commented Aug 12, 2022

Adds an optional cause to DOMException, put to use for operation specific WebCryptoAPI errors.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Aug 12, 2022
@panva panva added crypto Issues and PRs related to the crypto subsystem. webcrypto labels Aug 12, 2022
@panva panva force-pushed the add-domexception-cause branch from fe5da63 to ed6e2a7 Compare August 12, 2022 21:18
@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@legendecas legendecas left a 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

@legendecas
Copy link
Member

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.

@panva panva marked this pull request as draft August 13, 2022 06:00
@panva
Copy link
Member Author

panva commented Aug 13, 2022

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?

Comment on lines +57 to 58
cause
});
Copy link
Contributor

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)

Suggested change
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) {
Copy link
Contributor

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?

Suggested change
constructor(message = '', name = 'Error', cause = undefined) {
constructor(message = '', name = 'Error', options = { cause }) { // pseudocode

or

Suggested change
constructor(message = '', name = 'Error', cause = undefined) {
constructor(message = '', nameOrOptions = 'Error') {

@legendecas
Copy link
Member

Question: If I change the PR to not modify the constructor but still have the cause property, would that be ok now?

I'm unsure how the property cause would be defined on the DOMException. In ECMAScript, both name and message are own properties on the Error instances or the Error prototype. But they are attributes (with getters) on DOMException. cause may not present on an Error instance as an own property if the cause is not given. But if as an attribute with getters, it must be present and may not be distinguished from the case of DOMException(message, { cause: undefined }).

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 cause getter.

});
}

get cause() {
Copy link
Member

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 Errors, 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@panva
Copy link
Member Author

panva commented Sep 12, 2022

@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.

@jasnell
Copy link
Member

jasnell commented Sep 17, 2022

@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.

@jasnell
Copy link
Member

jasnell commented Sep 17, 2022

New alternative PR: #44703

@panva panva closed this Sep 17, 2022
@panva panva deleted the add-domexception-cause branch October 13, 2022 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Issues and PRs related to the crypto subsystem. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. webcrypto
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants