johnwmillr / LyricsGenius

Download song lyrics and metadata from Genius.com 🎶🎤
http://www.johnwmillr.com/scraping-genius-lyrics/
MIT License
892 stars 158 forks source link

Added raising HTTP and timeout errors #156

Closed allerter closed 3 years ago

allerter commented 3 years ago

I think LyricsGenius should raise HTTP and timeout errors since it's not the package's job to handle those errors. This would help with validating the token and informing the user of the result of their request. For example when the user requests something like genius.get_annotation(0) which is getting an item, the result of that request is a 404 error. On the other hand, if the user does a search like genius.search_genius_web('sddjwi') wich searches for an item, that request is valid and will have a 200 status code, but it won't have anything inside its sections. So I added raising HTTP and timeout errors. The timeout errors are raised through requests.exceptions.Timeout, and all HTTPErrors will be raised through requestrs.exceptions.HTTPError. Most of the time error descriptions is quite enough to figure out what's gone wrong. The user can also access the request's status code (two ways) and the error's description:

try:
    Genius('')
except HTTPError as e:
    print(e.errno) # status code
    print(e.args[0]) # status code
    print(e.args[1]) # error message

I also added the _validate_token() method and calling when the Genius object instantiates (should it be called in the API object's instantiation?). The method only requests a song from Genius API which returns 401 with proper description if the token is invalid. This resolves #105

I'd love to hear your thoughts on this.

allerter commented 3 years ago

@johnwmillr, this PR is ready to be merged. It's probably a good idea to resolve #158 and #159 too and then bump to 2.1.0 from 2.0.2

allerter commented 3 years ago

@johnwmillr, this PR is ready to be merged.

johnwmillr commented 3 years ago

Thank you for resolving these merge conflicts. I need to study up on handling these conflicts and merge the PRs without screwing up the commit history!