diff --git a/reolink/camera_api.py b/reolink/camera_api.py index 81fe27f..ff5afb9 100644 --- a/reolink/camera_api.py +++ b/reolink/camera_api.py @@ -7,7 +7,7 @@ from typing import Dict, List, Optional, Tuple, Any from . import typings from .software_version import SoftwareVersion -from .exceptions import CredentialsInvalidError, SnapshotIsNotValidFileTypeError, InvalidContentTypeError +from .exceptions import CredentialsInvalidError, SnapshotIsNotValidFileTypeError, InvalidContentTypeError, ApiError import traceback import re @@ -1654,16 +1654,21 @@ async def send(self, body, param=None, expected_content_type: Optional[str] = No async with self._aiohttp_session.get(url=self._url, params=param, allow_redirects=False) as response: _LOGGER.debug("%s/%s::send() HTTP Request params =%s", self.name, self._host, str(param).replace(self._password, "")) - json_data = await response.read() + _LOGGER.debug("%s/%s::send() HTTP Response status=%s content-type=(%s)", self.name, self._host, response.status, response.content_type) + json_data = await response.read() if param.get("cmd") == "Snap": _LOGGER_DATA.debug("%s/%s::send() HTTP Response data scrapped because it's too large", self.name, self._host) else: _LOGGER_DATA.debug("%s/%s::send() HTTP Response data: %s", self.name, self._host, json_data) + if response.status >= 400: + raise ApiError( + "API returned HTTP status ERROR code {}/{}".format(response.status, response.reason)) + if len(json_data) < 500 and response.content_type == 'text/html': if b'"detail" : "invalid user"' in json_data or \ b'"detail" : "login failed"' in json_data \ @@ -1684,15 +1689,20 @@ async def send(self, body, param=None, expected_content_type: Optional[str] = No str(param).replace(self._password, "")) _LOGGER.debug("%s/%s::send() HTTP Request body =%s", self.name, self._host, str(body).replace(self._password, "")) - json_data = await response.text() _LOGGER.debug("%s/%s::send() HTTP Response status=%s content-type=(%s)", self.name, self._host, response.status, response.content_type) + + json_data = await response.text() if param.get("cmd") == "Search" and len(json_data) > 500: _LOGGER_DATA.debug("%s/%s::send() HTTP Response data scrapped because it's too large", self.name, self._host) else: _LOGGER_DATA.debug("%s/%s::send() HTTP Response data: %s", self.name, self._host, json_data) + if response.status >= 400: + raise ApiError( + "API returned HTTP status ERROR code {}/{}".format(response.status, response.reason)) + if len(json_data) < 500 and response.content_type == 'text/html': if 'detail" : "invalid user' in json_data or 'detail" : "login failed' in json_data \ or 'detail" : "please login first' in json_data: diff --git a/reolink/exceptions.py b/reolink/exceptions.py index c274778..29b622e 100644 --- a/reolink/exceptions.py +++ b/reolink/exceptions.py @@ -4,6 +4,11 @@ class ReolinkError(Exception): pass +class ApiError(ReolinkError): + """Raised when API returns an error code""" + pass + + class InvalidContentTypeError(ReolinkError): """Raised when Snapshot command returns an invalid JPEG file""" pass