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

[python-pytrakt] Feature: Throw BadResponseException on JSON decode errors #181

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

glensc
Copy link
Contributor

@glensc glensc commented Jan 15, 2022

Many JSONDecodeError (from json module) are actually thrown because trakt.tv site responds with a status code that is not yet recognized by this library:

Add fallback logic to be able to catch this specific error from applications.

Fixes #146

trakt/core.py Outdated
try:
json_data = json.loads(response.content.decode('UTF-8', 'ignore'))
except JSONDecodeError as e:
raise errors.BadResponseException(f"Unable to parse JSON: {e}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should add a specific JsonDecodeException here?

@@ -32,6 +38,12 @@ def __str__(self):
return self.message


class BadResponseException(TraktException):
"""TraktException type to be raised when json could not be decoded"""
http_code = -1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used negative code for internal errors sp the positive numbers could be mapped to HTTP status codes

trakt/core.py Outdated
json_data = json.loads(response.content.decode('UTF-8', 'ignore'))
except JSONDecodeError as e:
ex = errors.BadResponseException(response)
ex.details = f"Unable to parse JSON: {e}"
Copy link
Contributor Author

@glensc glensc Jan 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or better add as constructor argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. seems better design, so: 6de70a5 & a4106cd.

@glensc
Copy link
Contributor Author

glensc commented Jan 15, 2022

Another fresh error:

The specific exception would allow to figure out url and status code (.response property).

@glensc glensc force-pushed the BadResponseException branch from a4106cd to 0e92938 Compare January 15, 2022 20:04
@glensc
Copy link
Contributor Author

glensc commented Jan 23, 2022

yet another json decode error #194

@glensc glensc force-pushed the BadResponseException branch from 0e92938 to 27d90b1 Compare April 14, 2022 10:16
@almightiest
Copy link

This would be great to have, please merge, thank you!

glensc added a commit to glensc/python-pytrakt that referenced this pull request Oct 19, 2022
@glensc
Copy link
Contributor Author

glensc commented Oct 23, 2022

Carried:

@glensc glensc changed the title Feature: Throw BadResponseException on JSON decode errors [python-pytrakt] Feature: Throw BadResponseException on JSON decode errors Nov 2, 2022
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

Successfully merging this pull request may close these issues.

trakt.init throws JSONDecodeError
2 participants