PokeAPI / pokepy

A Python wrapper for PokéAPI
https://pokeapi.co
BSD 3-Clause "New" or "Revised" License
127 stars 27 forks source link

404 Error handle #76

Closed wanda2003 closed 3 years ago

wanda2003 commented 3 years ago

Hi, first of all, it works great. But I'm curious: what would be the best way to handle 404 error when using .get_pokemon? I have tried requests.get("https://pokeapi.co/api/v2/pokemon") and do except requests.HTTPError but it seems that it's not a solution. The only way it could work is to use nested try-except but I don't think that's a good approach. I also try to look in the documentation but can't seems to find one address this.

Thank you for any suggestions.

Illustrate what I mean by nested try-except:

try:
    pk.get_pokemon()
except:
    try:
        pk.get_pokemon()
    except:
        try:
            pk.get_pokemon()
        except:
            return
Kronopt commented 3 years ago

Currently, the exception raised when a 404 is received is: beckett.exceptions.InvalidStatusCodeError: Received status code: 404, expected: (200, 201, 204)

You could do:

except beckett.exceptions.InvalidStatusCodeError as e:
    if e.status_code = 404:
        ...
wanda2003 commented 3 years ago

Thank you, I have read through beckett documentation and I manage to make it work.