lemonlabsuk / scala-uri

Simple scala library for building and parsing URIs
Other
305 stars 34 forks source link

Return Option[Url] from .parse() instead of potentially throwing exception #25

Closed chuck-g closed 5 years ago

chuck-g commented 5 years ago

I was surprised that the Url.parse() method didn't return an Option or Either, as this is a potentially error-prone operation, then I found UrlParser.getOrThrow() which explains the unwrapped return type. Instead of doing this and making the caller handle the exception, it would be better IMHO to return an Option[Url] or Either[Url]. You could also do this in a new method called Url.parseOption if you wanted to maintain current code.

Code Example:

val url = Url.parseOption("somethingBogus")
url mustEqual None
theon commented 5 years ago

There's not a huge amount of ways to get Url.parse to throw an exception (e.g. "somethingBogus" would return a RelativeUrl), but I do take your point!

I'm very happy to add a Url.parseOption method. Possibly also a Url.parseTry that returns a Try[Url], because the exception thrown from an invalid URL does contain useful information such as which character was the first unexpected character.

What do you think?

theon commented 5 years ago

Thanks again for raising this. It's now released under version 1.4.0. Let me know if you need anything else!

chuck-g commented 5 years ago

That's fantastic, and a really quick turnaround! It turned out to be quite a bit more invasive than I'd have thought, but thanks for your effort.

chuck-g commented 5 years ago

@GitCash send .01 bch to @theon

GitCash commented 5 years ago

Hey theon, user chuck-g tipped you 10000 bits in Bitcoin Cash ( ~ $5.13 ).

Click here to claim it!

You can also add the "thumbs down" reaction to chuck-g's comment above to prevent future tips.