Skip to content

Reference for stage 3 is-error#38251

Merged
hamishwillee merged 6 commits intomdn:mainfrom
Josh-Cena:error-iserror
Feb 24, 2025
Merged

Reference for stage 3 is-error#38251
hamishwillee merged 6 commits intomdn:mainfrom
Josh-Cena:error-iserror

Conversation

@Josh-Cena
Copy link
Copy Markdown
Member

Please see tc39/proposal-is-error#7 for implementation status. This is shipping flagged in Safari and by default in Chrome 134, but not much news from FF yet.

@Josh-Cena Josh-Cena requested a review from a team as a code owner February 20, 2025 06:44
@Josh-Cena Josh-Cena requested review from sideshowbarker and removed request for a team February 20, 2025 06:44
@github-actions github-actions bot added the Content:JS JavaScript docs label Feb 20, 2025
@Josh-Cena Josh-Cena requested review from hamishwillee and removed request for sideshowbarker February 20, 2025 06:44
@github-actions github-actions bot added the size/m [PR only] 51-500 LoC changed label Feb 20, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 20, 2025

Preview URLs

Flaws (2)

Note! 2 documents with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
Title: Error
Flaw count: 2

  • broken_links:
    • /en-US/docs/Glossary/serializable_object is ill cased
  • macros:
    • Macro produces link /en-US/docs/Glossary/serializable_object which is a redirect
External URLs (2)

URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError
Title: Error.isError()


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
Title: Array.isArray()

(comment last updated: 2025-02-24 05:44:43)

…ror/index.md

Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
…ror/index.md

Co-authored-by: Hamish Willee <hamishwillee@gmail.com>

## Description

`Error.isError()` checks if the passed value is an {{jsxref("Error")}}. It uses the same mechanism as {{jsxref("Array.isArray()")}}: it performs a _branded check_, similar to the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator, for a private property initialized by the {{jsxref("Error/Error", "Error()")}} constructor.
Copy link
Copy Markdown
Collaborator

@hamishwillee hamishwillee Feb 24, 2025

Choose a reason for hiding this comment

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

I think this is what you are saying?

Suggested change
`Error.isError()` checks if the passed value is an {{jsxref("Error")}}. It uses the same mechanism as {{jsxref("Array.isArray()")}}: it performs a _branded check_, similar to the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator, for a private property initialized by the {{jsxref("Error/Error", "Error()")}} constructor.
`Error.isError()` checks if the passed value is an {{jsxref("Error")}}. It does so by performing a _branded check_ for a private property initialized by the {{jsxref("Error/Error", "Error()")}} constructor.
This is the same mechanism used by {{jsxref("Array.isArray()")}}, which is in turn similar to the mechanism used by the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator.

So how does this work for DOMException ? Its constructor includes the same kind of private property as the error constructor?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also, possibly

Suggested change
`Error.isError()` checks if the passed value is an {{jsxref("Error")}}. It uses the same mechanism as {{jsxref("Array.isArray()")}}: it performs a _branded check_, similar to the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator, for a private property initialized by the {{jsxref("Error/Error", "Error()")}} constructor.
`Error.isError()` checks if the passed value is an {{jsxref("Error")}} or `DOMException`. It does so by performing a _branded check_ for a private property initialized by the {{jsxref("Error/Error", "Error()")}} constructor.
This is the same mechanism used by {{jsxref("Array.isArray()")}}, which is in turn similar to the mechanism used by the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

DOMException is supposed to behave like Error so I wouldn't want to imply that they are unrelated things and isError happens to check both. I'm not sure why the in operator reference is removed though—without it it's still hard to conceptualize what "branded check" and "private property" means.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Both suggestions still mentions in, it's just that this moved to the third sentence. The problem I am trying to fix is that it is hard to parse your sentence.

I'm fine with not mentioning "differentiating" the error types, so ignore my second suggestion and let's just consider the first one, which I will reproduce here:

Suggested change
`Error.isError()` checks if the passed value is an {{jsxref("Error")}}. It uses the same mechanism as {{jsxref("Array.isArray()")}}: it performs a _branded check_, similar to the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator, for a private property initialized by the {{jsxref("Error/Error", "Error()")}} constructor.
`Error.isError()` checks if the passed value is an {{jsxref("Error")}}. It does so by performing a _branded check_ for a private property initialized by the {{jsxref("Error/Error", "Error()")}} constructor.
This is the same mechanism used by {{jsxref("Array.isArray()")}}, which is in turn similar to the mechanism used by the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator.

And I am still interested in

So how does this work for DOMException ? Its constructor includes the same kind of private property as the error constructor?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So how does this work for DOMException ? Its constructor includes the same kind of private property as the error constructor?

I think I replied in one of the resolved conversations above but yeah that's exactly what happens.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you did, I missed it. Sorry. Thanks.

Comment on lines -45 to +52
`Array.isArray()` checks if the passed value is an {{jsxref("Array")}}. It does not check the value's prototype chain, nor does it rely on the `Array` constructor it is attached to. It returns `true` for any value that was created using the array literal syntax or the `Array` constructor. This makes it safe to use with cross-realm objects, where the identity of the `Array` constructor is different and would therefore cause [`instanceof Array`](/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) to fail.
`Array.isArray()` checks if the passed value is an {{jsxref("Array")}}. It performs a _branded check_, similar to the [`in`](/en-US/docs/Web/JavaScript/Reference/Operators/in) operator, for a private property initialized by the {{jsxref("Array/Array", "Array()")}} constructor.

See the article ["Determining with absolute accuracy whether or not a JavaScript object is an array"](https://web.mit.edu/jwalden/www/isArray.html) for more details.
It is a more robust alternative to [`instanceof Array`](/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) because it avoids false positives and false negatives:

- `Array.isArray()` rejects values that aren't actual `Array` instances, even if they have `Array.prototype` their prototype chain — `instanceof Array` would accept these as it does check the prototype chain.
- `Array.isArray()` accepts `Array` objects constructed in another realm — `instanceof Array` returns `false` for these because the identity of the `Array` constructor is different across realms.

`Array.isArray()` also rejects objects with `Array.prototype` in its prototype chain but aren't actual arrays, which `instanceof Array` would accept.
See the article ["Determining with absolute accuracy whether or not a JavaScript object is an array"](https://web.mit.edu/jwalden/www/isArray.html) for more details.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!

…ror/index.md

Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Copy link
Copy Markdown
Collaborator

@hamishwillee hamishwillee left a comment

Choose a reason for hiding this comment

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

LGTM. Any idea when the BCD will go in? Should merging be timed to match?

@Josh-Cena
Copy link
Copy Markdown
Member Author

Josh-Cena commented Feb 24, 2025

BCD is already there: mdn/browser-compat-data#25978 so it's just a matter of publish/deployment I think.

I wouldn't mind publishing docs with missing BCD in any case.

Copy link
Copy Markdown
Collaborator

@hamishwillee hamishwillee left a comment

Choose a reason for hiding this comment

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

I wouldn't mind publishing docs with missing BCD in any case.

In that case, we're good to go.

@hamishwillee hamishwillee merged commit 333cb25 into mdn:main Feb 24, 2025
8 checks passed
@Josh-Cena Josh-Cena deleted the error-iserror branch February 24, 2025 14:18
cssinate pushed a commit to cssinate/content that referenced this pull request Apr 11, 2025
@bsmth bsmth mentioned this pull request May 5, 2025
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:JS JavaScript docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants