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

Allowing throwing an exception with a specific message #1374

Open
annevk opened this issue Nov 8, 2023 · 5 comments
Open

Allowing throwing an exception with a specific message #1374

annevk opened this issue Nov 8, 2023 · 5 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest

Comments

@annevk
Copy link
Member

annevk commented Nov 8, 2023

What problem are you trying to solve?

fetch() throws a TypeError and callers distant from the fetch() call are having a hard time. As we cannot extend TypeError, the current best idea is to standardize on the message: whatwg/fetch#526.

If anyone has better ideas I'd very much appreciate them.

What solutions exist today?

None.

How would you solve it?

Allow create and throw to be passed an optional message argument.

Anything else?

This vaguely relates to #1024.

@annevk annevk added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Nov 8, 2023
@domenic
Copy link
Member

domenic commented Nov 8, 2023

You can add properties to the TypeError. That seems better?

(It's not currently possible in Web IDL, but we'd add that.)

@annevk
Copy link
Member Author

annevk commented Nov 8, 2023

Yeah maybe. @littledan @syg thoughts from a JS perspective?

Having said, I think that eventually standardizing messages would be good too, but if we can avoid them being the sole mechanism to rely on that would be a lot better.

@syg
Copy link
Contributor

syg commented Nov 8, 2023

You can add properties to the TypeError. That seems better?

Does this mean adding data properties to TypeError instances created by fetch()? I don't really see any issue with that, aside from unlikely compat issues around libraries that might be doing that already if the new property name is something common.

@domenic
Copy link
Member

domenic commented Nov 8, 2023

Does this mean adding data properties to TypeError instances created by fetch()?

Yep, that's what I meant.

(Or, I guess, own accessor properties, but own data properties would make more sense.)

@bakkot
Copy link
Contributor

bakkot commented Apr 3, 2024

Node uses error.code = "SOME_FIXED_STRING" and it's great. I would love to see that adopted on the web.

I don't see any problems with it from the JS side, though it's unlikely that errors thrown by JS would get such a property in the near future.

Do note that this can make optimizations which are formally (but not practically) unobservable suddenly be observable: right now if there's two adjacent steps which throw a TypeError and no user code runs between them, an engine can do those steps in whichever order is more convenient for it, and with this change would be required to do them in a particular order. But that's only very rarely going to be an issue, I think, and in any case users already depend on the unspecified message strings and so practically many of these things are observable anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest
Development

No branches or pull requests

4 participants