Feb-16-2018, 07:45 PM
(This post was last modified: Feb-16-2018, 07:45 PM by Gribouillis.)
(Feb-16-2018, 03:18 PM)league55 Wrote: I'm not sure whether I should shelve exception handling until I've got the more important and interesting parts of the application complete. What do you think?If you write a function named
validate_scheme()
, it means that you are now and then expecting invalid urls. The case where an error occurs in this call can be incorporated to this invalid case.In your situation, I would create my own exception type
InvalidUrl
and raise this exceptionclass InvalidUrl(Exception): pass def split_url(url): """Splits url for other validation functions. :returns: urllib.split.urlsplit object """ try: return urllib.parse.urlsplit(url) except Exception as exc: raise InvalidUrl from exc def validate_scheme(split_url): """Validates schema of url against a limited list of valid schemas. """ if split_url.scheme in schemes: return split_url else: raise InvalidUrl(("Invalid url scheme", split_url.scheme, "must be one of", schemes))Now the code that uses this can catch InvalidUrl and take corrective action when an invalid url is met.