drift-labs / driftpy

python sdk to interact with drift v2
https://drift-2.gitbook.io/driftpy-for-dummies/
56 stars 36 forks source link

Better handle or bubble up RPC errors #156

Open wphan opened 2 months ago

wphan commented 2 months ago

Account subscribers sometimes see unhelpful errors like this:

Traceback (most recent call last):
  File "/example/drift_sdk.py", line 75, in <module>
    asyncio.run(main())
  File "/Users/ww/.pyenv/versions/3.10.11/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/ww/.pyenv/versions/3.10.11/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/example/drift_sdk.py", line 46, in main
    await dc.subscribe()
  File "/example/drift_client.py", line 167, in subscribe
    await self.account_subscriber.subscribe()
  File "/example/venv/lib/python3.10/site-packages/driftpy/accounts/ws/drift_client.py", line 71, in subscribe
    ) = await find_all_market_and_oracles(self.program, data_and_slots=True)
  File "/example/venv/lib/python3.10/site-packages/driftpy/constants/config.py", line 142, in find_all_market_and_oracles
    perp_slot = int(parsed_resp.result["context"]["slot"])
AttributeError: 'Error' object has no attribute 'result'

if we dig in and print resp when it has no result, it's actually an RPC error:

Error(code=429, message='Too many requests for a specific RPC call, contact your app developer or support@rpcpool.com.', data=None, id=1)
wphan commented 2 months ago

https://github.com/drift-labs/driftpy/blob/7f24dea3da78bf489148dea233cc540c8e55746a/src/driftpy/accounts/get_accounts.py#L25

silently failing like this leads to really confusing user experience