From bc865615d445efb6aa2074688a160c020e2c54f0 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Thu, 23 May 2024 22:28:34 +1200 Subject: [PATCH] throw helpful exception when initiating a call fails --- src/audio_call_manager.py | 8 ++++++-- web.py | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/audio_call_manager.py b/src/audio_call_manager.py index 43bbf6b..d616d43 100644 --- a/src/audio_call_manager.py +++ b/src/audio_call_manager.py @@ -8,6 +8,10 @@ import RNS # todo allowlist/denylist for incoming calls +class CallFailedException(Exception): + pass + + class AudioCall: def __init__(self, link: RNS.Link, is_outbound: bool): @@ -131,7 +135,7 @@ class AudioCallManager: self.audio_calls.remove(audio_call) # attempts to initiate a call to the provided destination and returns the link hash on success - async def initiate(self, destination_hash: bytes, timeout_seconds: int = 15) -> bytes | None: + async def initiate(self, destination_hash: bytes, timeout_seconds: int = 15) -> bytes: # check if we have a path to the destination if not RNS.Transport.has_path(destination_hash): @@ -146,7 +150,7 @@ class AudioCallManager: # if we still don't have a path, we can't establish a link, so bail out if not RNS.Transport.has_path(destination_hash): - return None + raise CallFailedException("Could not find path to destination.") # create outbound destination to initiate audio calls server_identity = RNS.Identity.recall(destination_hash) diff --git a/web.py b/web.py index 2c9b53d..bdac932 100644 --- a/web.py +++ b/web.py @@ -227,15 +227,18 @@ class ReticulumWebChat: destination_hash = bytes.fromhex(destination_hash) # initiate audio call - link_hash = await self.audio_call_manager.initiate(destination_hash, timeout_seconds) - if link_hash is None: - return web.json_response({ - "message": "timed out initiating call", - }, status=503) + try: - return web.json_response({ - "hash": link_hash.hex(), - }) + link_hash = await self.audio_call_manager.initiate(destination_hash, timeout_seconds) + return web.json_response({ + "hash": link_hash.hex(), + }) + + except Exception as e: + + return web.json_response({ + "message": "Call Failed: {}".format(str(e)), + }, status=503) # handle websocket client for sending and receiving audio packets in a call @routes.get("/api/v1/calls/{audio_call_link_hash}/audio")