Skip to main content

What errors can you get from hyperlink.parse?

Mostly you get hyperlink.URLParseError, but you can occasionally get a ValueError as well.

I write a lot of code that uses hyperlink.parse. I want to make sure my code can handle all the exceptions which are thrown by hyperlink, and react accordingly.

The hyperlink URL parser is fairly robust, and handles quite a few strings that don’t look like URLs to me, for example:

>>> import hyperlink
>>> hyperlink.parse("")
DecodedURL(url=URL.from_text(''))

But it can throw exceptions:

Given that URLParseError and UnicodeDecodeError are subclasses of ValueError, it would be sensible to wrap calls to hyperlink.parse with try … except ValueError. Based on a cursory read of the hyperlink source code, I can’t see any places where it could throw a different exception type – but I missed the possibility of the Unicode error, so maybe there are more I haven’t found.