Skip to content

Commit 827550a

Browse files
committed
Merge branch 'BadResponseException'
Merged moogar0880/PyTrakt#181
2 parents 8a6d4f1 + 27d90b1 commit 827550a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

trakt/core.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import logging
77
import os
8+
from json import JSONDecodeError
89
from urllib.parse import urljoin
910

1011
import requests
@@ -15,6 +16,7 @@
1516
from requests_oauthlib import OAuth2Session
1617
from datetime import datetime, timedelta, timezone
1718
from trakt import errors
19+
from trakt.errors import BadResponseException
1820

1921
__author__ = 'Jon Nappi'
2022
__all__ = ['Airs', 'Alias', 'Comment', 'Genre', 'get', 'delete', 'post', 'put',
@@ -529,7 +531,12 @@ def _handle_request(self, method, url, data=None):
529531
raise self.error_map[response.status_code](response)
530532
elif response.status_code == 204: # HTTP no content
531533
return None
532-
json_data = json.loads(response.content.decode('UTF-8', 'ignore'))
534+
535+
try:
536+
json_data = json.loads(response.content.decode('UTF-8', 'ignore'))
537+
except JSONDecodeError as e:
538+
raise BadResponseException(response, f"Unable to parse JSON: {e}")
539+
533540
return json_data
534541

535542
def get(self, f):

trakt/errors.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66

77
__author__ = 'Jon Nappi'
88
__all__ = [
9+
# Base Exception
910
'TraktException',
11+
12+
# Errors for use by PyTrakt
13+
'BadResponseException',
14+
15+
# Exceptions by HTTP status code
1016
'BadRequestException',
1117
'OAuthException',
1218
'ForbiddenException',
@@ -32,6 +38,16 @@ def __str__(self):
3238
return self.message
3339

3440

41+
class BadResponseException(TraktException):
42+
"""TraktException type to be raised when json could not be decoded"""
43+
http_code = -1
44+
message = "Bad Response - Response could not be parsed"
45+
46+
def __init__(self, response=None, details=None):
47+
super().__init__(response)
48+
self.details = details
49+
50+
3551
class BadRequestException(TraktException):
3652
"""TraktException type to be raised when a 400 return code is received"""
3753
http_code = 400

0 commit comments

Comments
 (0)