Avoid nesting try/excepts
This commit is contained in:
@@ -149,33 +149,31 @@ async def parse_telephony_websocket(websocket: WebSocket):
|
|||||||
user_id = call_data["body"]["user_id"]
|
user_id = call_data["body"]["user_id"]
|
||||||
"""
|
"""
|
||||||
# Read first two messages
|
# Read first two messages
|
||||||
start_data = websocket.iter_text()
|
message_stream = websocket.iter_text()
|
||||||
|
first_message = {}
|
||||||
|
second_message = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# First message
|
# First message - required
|
||||||
try:
|
first_message_raw = await message_stream.__anext__()
|
||||||
first_message_raw = await start_data.__anext__()
|
logger.trace(f"First message: {first_message_raw}")
|
||||||
logger.trace(f"First message: {first_message_raw}")
|
first_message = json.loads(first_message_raw) if first_message_raw else {}
|
||||||
try:
|
except json.JSONDecodeError:
|
||||||
first_message = json.loads(first_message_raw)
|
pass
|
||||||
except json.JSONDecodeError:
|
except StopAsyncIteration:
|
||||||
first_message = {}
|
raise ValueError("WebSocket closed before receiving telephony handshake messages")
|
||||||
except StopAsyncIteration:
|
|
||||||
raise ValueError("WebSocket closed before receiving telephony handshake messages")
|
|
||||||
|
|
||||||
# Second message
|
try:
|
||||||
try:
|
# Second message - optional, some providers may only send one
|
||||||
second_message_raw = await start_data.__anext__()
|
second_message_raw = await message_stream.__anext__()
|
||||||
logger.trace(f"Second message: {second_message_raw}")
|
logger.trace(f"Second message: {second_message_raw}")
|
||||||
try:
|
second_message = json.loads(second_message_raw) if second_message_raw else {}
|
||||||
second_message = json.loads(second_message_raw)
|
except json.JSONDecodeError:
|
||||||
except json.JSONDecodeError:
|
pass
|
||||||
second_message = {}
|
except StopAsyncIteration:
|
||||||
except StopAsyncIteration:
|
logger.warning("Only received one WebSocket message, expected two")
|
||||||
# Only one message received - use it for detection
|
|
||||||
logger.warning("Only received one WebSocket message, expected two")
|
|
||||||
second_message = {}
|
|
||||||
|
|
||||||
|
try:
|
||||||
# Try auto-detection on both messages
|
# Try auto-detection on both messages
|
||||||
detected_type_first = _detect_transport_type_from_message(first_message)
|
detected_type_first = _detect_transport_type_from_message(first_message)
|
||||||
detected_type_second = _detect_transport_type_from_message(second_message)
|
detected_type_second = _detect_transport_type_from_message(second_message)
|
||||||
|
|||||||
Reference in New Issue
Block a user