From c467b5fd033413c9f6ccac8ec7fdf3bcb4fc9336 Mon Sep 17 00:00:00 2001 From: Rhea Parekh Date: Fri, 11 May 2018 03:21:27 -0700 Subject: [PATCH] Matrix: Handle login and joining room errors. --- zulip/integrations/matrix/matrix_bridge.py | 32 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/zulip/integrations/matrix/matrix_bridge.py b/zulip/integrations/matrix/matrix_bridge.py index 441d58f..9169227 100644 --- a/zulip/integrations/matrix/matrix_bridge.py +++ b/zulip/integrations/matrix/matrix_bridge.py @@ -12,6 +12,29 @@ from typing import Any, Callable, Dict from matrix_bridge_config import config from matrix_client.api import MatrixRequestError from matrix_client.client import MatrixClient +from requests.exceptions import MissingSchema + +def matrix_login(matrix_client: Any, matrix_config: Dict[str, Any]) -> None: + try: + matrix_client.login_with_password(matrix_config["username"], + matrix_config["password"]) + except MatrixRequestError as e: + if e.code == 403: + sys.exit("Bad username or password.") + else: + sys.exit("Check if your server details are correct.") + except MissingSchema as e: + sys.exit("Bad URL format.") + +def matrix_join_room(matrix_client: Any, matrix_config: Dict[str, Any]) -> Any: + try: + room = matrix_client.join_room(matrix_config["room_id"]) + return room + except MatrixRequestError as e: + if e.code == 403: + sys.exit("Room ID/Alias in the wrong format") + else: + sys.exit("Couldn't find room.") def die(signal: int, frame: FrameType) -> None: # We actually want to exit, so run os._exit (so as not to be caught and restarted) @@ -102,10 +125,11 @@ if __name__ == '__main__': site=zulip_config["site"]) matrix_client = MatrixClient(matrix_config["host"]) - # TODO this lacks the proper error handling - matrix_client.login_with_password(matrix_config["username"], - matrix_config["password"]) - room = matrix_client.join_room(matrix_config["room_id"]) + # Login to Matrix + matrix_login(matrix_client, matrix_config) + # Join a room in Matrix + room = matrix_join_room(matrix_client, matrix_config) + room.add_listener(matrix_to_zulip(zulip_client, zulip_config, matrix_config)) print("Starting listener thread on Matrix client")