Skip to content

Commit 70e81dc

Browse files
authored
Merge pull request #30 from hectorespert/capture_login_exception
Improve login exception
2 parents 55f8b5c + 4b58c83 commit 70e81dc

4 files changed

Lines changed: 9 additions & 7 deletions

File tree

oligo/asyncio/asynciber.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def __init__(self) -> None:
3131
self.__session = None
3232

3333
async def close(self):
34-
await self.__session.close()
34+
if self.__session:
35+
await self.__session.close()
3536

3637
async def __request(
3738
self, path: str, data: Optional[Union[list, dict]] = None
@@ -73,8 +74,9 @@ async def login(self, user: str, password: str) -> bool:
7374
]
7475
data = await self.__request(LOGIN_URL, data=payload)
7576
if data["success"] != "true":
77+
await self.__session.close()
7678
self.__session = None
77-
raise LoginException()
79+
raise LoginException(user)
7880
return True
7981

8082
async def measurement(self) -> dict:

oligo/exception.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def __init__(self, status_code):
88

99

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

1414

1515
class SessionException(IberException):

oligo/requests/iber.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def login(self, user, password, session=Session()):
4949
json_response = response.json()
5050
if json_response["success"] != "true":
5151
self.__session = None
52-
raise LoginException()
52+
raise LoginException(user)
5353

5454
def __check_session(self):
5555
if not self.__session:

tests/test_exception.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def test_message(self):
1313
class TestLoginException(unittest.TestCase):
1414

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

1919

2020
class TestSessionException(unittest.TestCase):

0 commit comments

Comments
 (0)