Skip to content

Commit 55dfe43

Browse files
author
Shlomi Kushchi
authored
Merge pull request #285 from alpacahq/verbose_stacktrace
Increase ws consume() exceptions verbosity
2 parents 949956f + 98227aa commit 55dfe43

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ same `channel_pat` will overwrite the old handler.
285285
Deregisters the event handler function that was previously registered via `on` or
286286
`register` method.
287287

288+
#### Debugging
289+
Websocket exceptions may occur during execution.
290+
It will usually happen during the `consume()` method, which basically is the
291+
websocket steady-state.<br>
292+
exceptions during the consume method may occur due to:
293+
- server disconnections
294+
- error while handling the response data
295+
296+
We handle the first issue by reconnecting the websocket every time there's a disconnection.
297+
The second issue, is usually a user's code issue. To help you find it, we added a flag to the
298+
StreamConn object called `debug`. It is set to False by default, but you can turn it on to get a more
299+
verbose logs when this exception happens.
300+
Turn it on like so `StreamConn(debug=True)`
301+
288302
## Logging
289303
You should define a logger in your app in order to make sure you get all the messages from the different components.<br>
290304
It will help you debug, and make sure you don't miss issues when they occur.<br>
@@ -293,7 +307,7 @@ The simplest way to define a logger, if you have no experience with the python l
293307
import logging
294308
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
295309
```
296-
---
310+
297311
# Polygon API Service
298312

299313
Alpaca's API key ID can be used to access Polygon API, the documentation for

alpaca_trade_api/stream2.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import os
44
import re
5+
import traceback
56
from asyncio import CancelledError
67

78
import websockets
@@ -183,7 +184,9 @@ def __init__(
183184
secret_key: str = None,
184185
base_url: URL = None,
185186
data_url: URL = None,
186-
data_stream: str = None):
187+
data_stream: str = None,
188+
debug: bool = False
189+
):
187190
self._key_id, self._secret_key, _ = get_credentials(key_id, secret_key)
188191
self._base_url = base_url or get_base_url()
189192
self._data_url = data_url or get_data_url()
@@ -196,6 +199,7 @@ def __init__(
196199
else:
197200
_data_stream = 'alpacadatav1'
198201
self._data_stream = _data_stream
202+
self._debug = debug
199203

200204
self.trading_ws = _StreamConn(self._key_id,
201205
self._secret_key,
@@ -293,6 +297,8 @@ def run(self, initial_channels: List[str] = []):
293297
except Exception as e:
294298
m = 'consume cancelled' if isinstance(e, CancelledError) else e
295299
logging.error(f"error while consuming ws messages: {m}")
300+
if self._debug:
301+
traceback.print_exc()
296302
loop.run_until_complete(self.close(should_renew))
297303
if loop.is_running():
298304
loop.close()

0 commit comments

Comments
 (0)