-
Notifications
You must be signed in to change notification settings - Fork 44
Handle different 4xx-errors #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: version-2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,14 @@ | |
| ''' | ||
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| import pickle | ||
|
|
||
| import requests | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Error(Exception): | ||
| ''' Verisure session error ''' | ||
|
|
@@ -19,6 +22,9 @@ class RequestError(Error): | |
|
|
||
| class LoginError(Error): | ||
| ''' Login failed ''' | ||
| def __init__(self, text, status_code = None): | ||
| super().__init__( | ||
| f'Login error, status code: {status_code} - Data: {text}') | ||
|
|
||
|
|
||
| class LogoutError(Error): | ||
|
|
@@ -102,20 +108,32 @@ def wrapper(url, *args, **kwargs): | |
| try: | ||
| response = function(base_url+url, *args, **kwargs) | ||
| if response.status_code >= 500: | ||
| _LOGGER.warning(f'Error from {base_url}: {response.status_code} - {response.text}') | ||
| last_exception = ResponseError(response.status_code, response.text) | ||
| self._base_urls.reverse() | ||
| continue | ||
| if response.status_code >= 400: | ||
| last_exception = LoginError(response.text) | ||
| break | ||
| _LOGGER.warning(f'Error from {base_url}: {response.status_code} - {response.text}') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logging here is not adding any real value, as there will be an error anyway |
||
| if "Session has expired" in response.text: | ||
| raise LoginError(response.text, response.status_code) | ||
| elif "Username/password does not match any valid login" in response.text: | ||
| raise ResponseError(response.status_code, response.text) | ||
|
Comment on lines
+117
to
+120
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4** indicates an authorization error. If we raise ResponseError here, we are not able to respond properly. |
||
|
|
||
| last_exception = LoginError(response.text, response.status_code) | ||
| self._base_urls.reverse() | ||
| continue | ||
| if response.status_code == 200: | ||
| _LOGGER.debug(f'OK from {base_url}: {response.status_code}') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logging here is not adding any real value |
||
| if "SYS_00004" in response.text: | ||
| self._base_urls.reverse() | ||
| continue | ||
| return response | ||
| _LOGGER.debug(f'Unknown status from {base_url}: {response.status_code} - {response.text}') | ||
|
|
||
| except requests.exceptions.RequestException as ex: | ||
| _LOGGER.debug(f'Exception from {base_url}: {ex}') | ||
| last_exception = RequestError(str(ex)) | ||
| _LOGGER.debug(f'Switching active base_url') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logging here is not adding any real value |
||
| self._base_urls.reverse() | ||
| raise last_exception | ||
| return wrapper | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging a preemptive warning will only spam the logs if the second url is successful