-
Notifications
You must be signed in to change notification settings - Fork 141
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
Consider adding non-throwing URL.parse(input, base) #372
Comments
I read through the linked thread but don't see a convincing rationale.
I would say at the layer of the Even if the implementations don't do this at the moment, the benefit of an exception is that the error can include a nice error message explaining why the URL is invalid, e.g. "Character \u1234 is not allowed in URL" or "Port segment can only contain numbers, found character 'a'". If the URL truly comes from user input, that is a nicer message to display to the user than "Invalid URL". |
The scenario is mostly about parsing URLs found in HTML and such. You could probably do some error signaling out-of-band, e.g., the developer console found in browsers, but that might not be relevant to everyone. Also, note that this isn't about changing the default. This is solely about providing a wrapper, but it does seem that nobody has really been requesting this, so wontfixing might be fine. |
I still think this, more so than many of the API additions proposed, is a good idea with no real downsides. IMO parser APIs should always provide a mode that returns a failure sentinel instead of throwing an exception. See e.g. https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions-and-performance#try-parse-pattern However, I admit that we haven't seen a lot of demand---probably because try/catch is easy enough to use. |
Yeah, if someone wants to add it and has some implementer backing I'd certainly be supportive. |
https://twitter.com/kilianvalkhof/status/1765312128188088454 and responses (also in WHATWG Chat by Tab) shows there's still appetite for this. It's rather straightforward to add too. I think we should do it. |
I think this would be a nice and easy feature to add. Before we commit to implementing it, I want to raise the possibility of returning an object instead of a URL or null. In any case, I'm supportive of a non-throwing option to parse URLs. |
I like that idea, but I suspect a separate method would be better if we ever decided to do that as URL parsers are highly optimized and you'd probably want a separate code path for the detailed error URL parser. Or at least allow for the possibility of one. |
Tests: web-platform-tests/wpt#45248. Fixes #372.
Tests: web-platform-tests/wpt#45248. Fixes #372.
Tests: web-platform-tests/wpt#45248. Fixes #372.
@annevk I know it's a bit late, but just as there was discuss of putting in |
Thanks for bringing that to my attention. I don't see the need for parity with |
I wrote a cross-platform polyfill: https://gist.github.com/eligrey/282cb2bef74d81e73bee7e836c82d0a5 |
Rather than throwing it would return null. This allows somewhat cleaner code.
See jsdom/jsdom#2146 for rationale.
The text was updated successfully, but these errors were encountered: