Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion trakt/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import os
from json import JSONDecodeError
from urllib.parse import urljoin

import requests
Expand All @@ -15,6 +16,7 @@
from requests_oauthlib import OAuth2Session
from datetime import datetime, timedelta, timezone
from trakt import errors
from trakt.errors import BadResponseException

__author__ = 'Jon Nappi'
__all__ = ['Airs', 'Alias', 'Comment', 'Genre', 'get', 'delete', 'post', 'put',
Expand Down Expand Up @@ -529,7 +531,12 @@ def _handle_request(self, method, url, data=None):
raise self.error_map[response.status_code](response)
elif response.status_code == 204: # HTTP no content
return None
json_data = json.loads(response.content.decode('UTF-8', 'ignore'))

try:
json_data = json.loads(response.content.decode('UTF-8', 'ignore'))
except JSONDecodeError as e:
raise BadResponseException(response, f"Unable to parse JSON: {e}")

return json_data

def get(self, f):
Expand Down
16 changes: 16 additions & 0 deletions trakt/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

__author__ = 'Jon Nappi'
__all__ = [
# Base Exception
'TraktException',

# Errors for use by PyTrakt
'BadResponseException',

# Exceptions by HTTP status code
'BadRequestException',
'OAuthException',
'ForbiddenException',
Expand All @@ -32,6 +38,16 @@ 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

message = "Bad Response - Response could not be parsed"

def __init__(self, response=None, details=None):
super().__init__(response)
self.details = details


class BadRequestException(TraktException):
"""TraktException type to be raised when a 400 return code is received"""
http_code = 400
Expand Down