Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

404 Error handle #76

Closed
wanda2003 opened this issue Nov 28, 2021 · 2 comments
Closed

404 Error handle #76

wanda2003 opened this issue Nov 28, 2021 · 2 comments

Comments

@wanda2003
Copy link

wanda2003 commented Nov 28, 2021

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
Copy link
Member

Kronopt commented Nov 28, 2021

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants