Skip to content

Commit dd6ef14

Browse files
authored
Merge pull request #2 from YinYin-blip/streamingapi
improved general exceptions to be more specific
2 parents c2393a5 + 0b10568 commit dd6ef14

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

luno_python/stream_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
class OutOfOrderMessageException(Exception):
2121
pass
2222

23+
class MarketInitialisationException(Exception):
24+
pass
25+
2326

2427
def _flatten_orders(orders, reverse):
2528
return sorted(orders.values(), key=lambda o: o.price, reverse=reverse)
@@ -38,7 +41,7 @@ def _decrement_trade(orders: Dict[str, Order], order_id: str, volume: Decimal):
3841
class _MarketStreamState:
3942
def __init__(self, first: dict):
4043
if first is None:
41-
raise Exception("Unable to use empty message to initialise market state")
44+
raise MarketInitialisationException("Unable to initialise market state from an empty message")
4245

4346
def conv_message(msg):
4447
return Order(
@@ -136,8 +139,8 @@ async def _read_from_websocket(ws, pair: Pair, update_f: StateUpdate):
136139
async for message in ws:
137140
try:
138141
body = json.loads(message)
139-
except ValueError:
140-
raise Exception(message)
142+
except ValueError as e:
143+
raise ValueError(f"Invalid JSON received: {message}") from e
141144

142145
if body == "": # Empty update, used as keepalive
143146
body = None
@@ -177,7 +180,7 @@ async def stream_market(
177180
:param update_callback: an StateUpdate function that will be called with new updates.
178181
"""
179182
if len(pair) != 6:
180-
raise Exception("Invalid pair")
183+
raise ValueError(f"pair must be length 6, got '{pair}'")
181184

182185
p = Pair(pair[:3].upper(), pair[3:].upper())
183186
url = '/'.join([base_url, 'api/1/stream', p.base + p.counter])

0 commit comments

Comments
 (0)