handle login error

This commit is contained in:
Frank Becker 2020-02-06 23:05:46 +01:00
parent 665007a4bb
commit 0738e78e0d
1 changed files with 21 additions and 13 deletions

14
bot.py
View File

@ -12,7 +12,8 @@ import urllib.parse
from importlib import reload from importlib import reload
import requests import requests
from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LogoutResponse, LogoutError from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LogoutResponse, LogoutError, \
LoginError
# Couple of custom exceptions # Couple of custom exceptions
@ -266,7 +267,12 @@ class Bot:
async def run(self): async def run(self):
if not self.client.access_token: if not self.client.access_token:
await self.client.login(self.matrix_pass) login_response = await self.client.login(self.matrix_pass)
if isinstance(login_response, LoginError):
print(f"Failed to login: {login_response.message}")
return
last_16 = self.client.access_token[-16:] last_16 = self.client.access_token[-16:]
print(f"Logged in with password, access token: ...{last_16}") print(f"Logged in with password, access token: ...{last_16}")
@ -296,6 +302,7 @@ class Bot:
async def shutdown(self): async def shutdown(self):
if self.client.logged_in:
logout = await self.client.logout() logout = await self.client.logout()
if isinstance(logout, LogoutResponse): if isinstance(logout, LogoutResponse):
@ -309,7 +316,8 @@ class Bot:
else: else:
logout: LogoutError logout: LogoutError
print(f"Logout unsuccessful. msg: {logout.message}") print(f"Logout unsuccessful. msg: {logout.message}")
else:
await self.client.client_session.close()
bot = Bot() bot = Bot()
bot.init() bot.init()