matthewgilbert / pdblp

pandas wrapper for Bloomberg Open API
MIT License
242 stars 67 forks source link

Returning Bloomberg blpapi errors. #30

Closed ndominguez4219 closed 6 years ago

ndominguez4219 commented 6 years ago

It would be very useful to be able to use the errors issued by blpapi (e.g., ConnectionError, etc). Is there a way to access these through the API. An example would be very useful.

matthewgilbert commented 6 years ago

Unless I am misunderstanding your question, this would be done the same way you handle catching and returning errors with most python code; wrap the desired block in a try-except block.

import pdblp
con = pdblp.BCon(port=5555)

try:
    con.start()
except ConnectionError as e:
    print("\nI have caught the ConnectionError and will do some things"
          "before raising it\n")
    raise e
27MAR2018_09:30:08.414 4606:140323755443968 WARN blpapi_platformtransporttcp.cpp:137 blpapi.session.transporttcp.{1}.<localhost:5555> 127.0.0.1:5555, session pool state=Failed 

27MAR2018_09:30:08.414 4606:140323755443968 WARN blpapi_platformcontroller.cpp:508 blpapi.session.platformcontroller.{1} Platform failed 1 consecutive connect attempts, stopped trying to reconnect. { PlatformId=0 }  
INFO:root:Failed to start session.

I have caught the ConnectionError and will do some things before raising it

---------------------------------------------------------------------------
ConnectionError                           Traceback (most recent call last)
<ipython-input-1-334e27117f52> in <module>()
      7 except ConnectionError as e:
      8     print("\nI have caught the ConnectionError and will do some things before raising it\n")
----> 9     raise e

<ipython-input-1-334e27117f52> in <module>()
      4 
      5 try:
----> 6     con.start()
      7 except ConnectionError as e:
      8     print("\nI have caught the ConnectionError and will do some things before raising it\n")

/home/matthew/Projects/pdblp/pdblp/pdblp.py in start(self)
     83         if not self.session.start():
     84             logging.info("Failed to start session.")
---> 85             raise ConnectionError("Could not start a blpapi.session")
     86         self.session.nextEvent()
     87         # Open service to get historical data from

ConnectionError: Could not start a blpapi.session

If for whatever reason you wanted to persist the error outside of the try-except block you can assign it so it doesn't go out of scope

try:
    con.start()
except ConnectionError as e:
    persist_e = e

persist_e
 ConnectionError('Could not start a blpapi.session')
ndominguez4219 commented 6 years ago

Works, thanks. Part of the issue is my relative newness with python. Best.