Skip to content

Commit 89580e4

Browse files
authored
Handle 'Attempt to decode JSON with unexpected mimetype: ' exceptions (#41)
1 parent d7d1b0e commit 89580e4

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

pyhoma/client.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
from __future__ import annotations
33

44
import urllib.parse
5+
from json import JSONDecodeError
56
from types import TracebackType
67
from typing import Any, Dict, List, Optional, Type, Union
78

89
import humps
910
from aiohttp import ClientResponse, ClientSession
1011

11-
from pyhoma.exceptions import BadCredentialsException, TooManyRequestsException
12+
from pyhoma.exceptions import (
13+
BadCredentialsException,
14+
NotAuthenticatedException,
15+
TooManyRequestsException,
16+
)
1217
from pyhoma.models import Command, Device, Event, Execution, Scenario, State
1318

1419
JSON = Union[Dict[str, Any], List[Dict[str, Any]]]
@@ -217,28 +222,29 @@ async def check_response(response: ClientResponse) -> None:
217222
""" Check the response returned by the TaHoma API"""
218223
if response.status == 200:
219224
return
220-
# 401
221-
# {'errorCode': 'AUTHENTICATION_ERROR',
222-
# 'error': 'Too many requests, try again later : login with xxx@xxx.tld'}
223-
# 'error': 'Bad credentials'}
224-
# 'error': 'Your setup cannot be accessed through this application'}
225-
result = await response.json()
226-
if response.status == 401:
227-
if result.get("errorCode") == "AUTHENTICATION_ERROR":
228-
message = result.get("error")
229225

230-
if "Too many requests" in message:
231-
raise TooManyRequestsException(message)
226+
try:
227+
result = await response.json(content_type=None)
228+
except JSONDecodeError:
229+
result = await response.text()
230+
raise Exception(
231+
f"Unknown error while requesting {response.url}. {response.status} - {result}"
232+
)
232233

233-
if "Your setup cannot be accessed through this application" in message:
234-
raise Exception(message)
234+
if result.get("errorCode"):
235+
message = result.get("error")
235236

236-
if "Bad credentials" in message:
237-
raise BadCredentialsException(message)
237+
# {"errorCode": "AUTHENTICATION_ERROR",
238+
# "error": "Too many requests, try again later : login with xxx@xxx.tld"}
239+
if "Too many requests" in message:
240+
raise TooManyRequestsException(message)
238241

239-
raise Exception(message)
242+
# {"errorCode": "AUTHENTICATION_ERROR", "error": "Bad credentials"}
243+
if message == "Bad credentials":
244+
raise BadCredentialsException(message)
240245

241-
if 400 < response.status < 500:
242-
message = result.get("error")
246+
# {"errorCode": "RESOURCE_ACCESS_DENIED", "error": "Not authenticated"}
247+
if message == "Not authenticated":
248+
raise NotAuthenticatedException(message)
243249

244-
raise Exception(message)
250+
raise Exception(message if message else result)

pyhoma/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ class TooManyRequestsException(Exception):
44

55
class BadCredentialsException(Exception):
66
pass
7+
8+
9+
class NotAuthenticatedException(Exception):
10+
pass

0 commit comments

Comments
 (0)