Skip to content

Commit

Permalink
Merge pull request #30 from hectorespert/capture_login_exception
Browse files Browse the repository at this point in the history
Improve login exception
  • Loading branch information
hectorespert authored Jan 25, 2022
2 parents 55f8b5c + 4b58c83 commit 70e81dc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions oligo/asyncio/asynciber.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def __init__(self) -> None:
self.__session = None

async def close(self):
await self.__session.close()
if self.__session:
await self.__session.close()

async def __request(
self, path: str, data: Optional[Union[list, dict]] = None
Expand Down Expand Up @@ -73,8 +74,9 @@ async def login(self, user: str, password: str) -> bool:
]
data = await self.__request(LOGIN_URL, data=payload)
if data["success"] != "true":
await self.__session.close()
self.__session = None
raise LoginException()
raise LoginException(user)
return True

async def measurement(self) -> dict:
Expand Down
4 changes: 2 additions & 2 deletions oligo/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def __init__(self, status_code):


class LoginException(IberException):
def __init__(self):
super().__init__('Login error, bad login')
def __init__(self, username):
super().__init__(f'Unable to log in with user {username}')


class SessionException(IberException):
Expand Down
2 changes: 1 addition & 1 deletion oligo/requests/iber.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def login(self, user, password, session=Session()):
json_response = response.json()
if json_response["success"] != "true":
self.__session = None
raise LoginException()
raise LoginException(user)

def __check_session(self):
if not self.__session:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def test_message(self):
class TestLoginException(unittest.TestCase):

def test_message(self):
login_exception = LoginException()
self.assertEqual('Login error, bad login', login_exception.args[0])
login_exception = LoginException("pepe")
self.assertEqual('Unable to log in with user pepe', login_exception.args[0])


class TestSessionException(unittest.TestCase):
Expand Down

0 comments on commit 70e81dc

Please sign in to comment.