Skip to content

Commit

Permalink
Handle 'Attempt to decode JSON with unexpected mimetype: ' exceptions (
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl committed Aug 26, 2020
1 parent d7d1b0e commit 89580e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
46 changes: 26 additions & 20 deletions pyhoma/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
from __future__ import annotations

import urllib.parse
from json import JSONDecodeError
from types import TracebackType
from typing import Any, Dict, List, Optional, Type, Union

import humps
from aiohttp import ClientResponse, ClientSession

from pyhoma.exceptions import BadCredentialsException, TooManyRequestsException
from pyhoma.exceptions import (
BadCredentialsException,
NotAuthenticatedException,
TooManyRequestsException,
)
from pyhoma.models import Command, Device, Event, Execution, Scenario, State

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

if "Too many requests" in message:
raise TooManyRequestsException(message)
try:
result = await response.json(content_type=None)
except JSONDecodeError:
result = await response.text()
raise Exception(
f"Unknown error while requesting {response.url}. {response.status} - {result}"
)

if "Your setup cannot be accessed through this application" in message:
raise Exception(message)
if result.get("errorCode"):
message = result.get("error")

if "Bad credentials" in message:
raise BadCredentialsException(message)
# {"errorCode": "AUTHENTICATION_ERROR",
# "error": "Too many requests, try again later : login with xxx@xxx.tld"}
if "Too many requests" in message:
raise TooManyRequestsException(message)

raise Exception(message)
# {"errorCode": "AUTHENTICATION_ERROR", "error": "Bad credentials"}
if message == "Bad credentials":
raise BadCredentialsException(message)

if 400 < response.status < 500:
message = result.get("error")
# {"errorCode": "RESOURCE_ACCESS_DENIED", "error": "Not authenticated"}
if message == "Not authenticated":
raise NotAuthenticatedException(message)

raise Exception(message)
raise Exception(message if message else result)
4 changes: 4 additions & 0 deletions pyhoma/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ class TooManyRequestsException(Exception):

class BadCredentialsException(Exception):
pass


class NotAuthenticatedException(Exception):
pass

0 comments on commit 89580e4

Please sign in to comment.