matthewgilbert / pdblp

pandas wrapper for Bloomberg Open API
MIT License
241 stars 68 forks source link

Error Handling #33

Closed henrywuhu closed 6 years ago

henrywuhu commented 6 years ago

Hey.

I am doing a program to fetch data from bloomberg. I read a csv file that contains tickers. If a ticker is invalid, how can i do the error handling.

For example USBX@ is not a valid security my code is the following:

con = pdblp.BCon(debug=False, port=8194)
con.start()
try:
    x = con.bdh('USBX@ Curncy', 'PX_BID', '20180509','20180509')
except ValueError:
    print("ERROR")

for example if I run it on Pycharm I get the following string of exceptions. In this I can check that there is an invalid security, but how can I get the subcategory error immediately from try except using ValueError or NameError

Traceback (most recent call last):
  File "C:/Users/hwuhu/PycharmProjects/PRUEBA/PRUEBBA.py", line 22, in <module>
    x = con.bdh('USBX@ Curncy', 'PX_BID', '20180509','20180509')
  File "c:\users\hwuhu\pdblp\pdblp\pdblp.py", line 165, in bdh
    elms, ovrds)
  File "c:\users\hwuhu\pdblp\pdblp\pdblp.py", line 203, in _bdh_list
    raise Exception(msg)
Exception: HistoricalDataResponse = {
    securityData = {
        security = "USBX@ Curncy"
        eidData[] = {
        }
        sequenceNumber = 0
        securityError = {
            source = "4013::bbdbh5"
            code = 15
            category = "BAD_SEC"
            message = "Unknown/Invalid securityInvalid Security [nid:4013] "
            subcategory = "INVALID_SECURITY"
        }
        fieldExceptions[] = {
        }
        fieldData[] = {
        }
    }
}
matthewgilbert commented 6 years ago

Currently a general Exception is being raised, so you would have to catch that not a ValueError.

try:
    x = con.bdh('USBX@ Curncy', 'PX_BID', '20180509','20180509')
except Exception:
    print("ERROR")

ERROR

I do agree that raising a more concise Error would be preferable though.

matthewgilbert commented 6 years ago

As of commit 240fd0fd747aa94fcba5ec80461b86748191ab98 you should now be able to use your code above to catch these types of errors, e.g.

try:
    x = con.bdh('USBX@ Curncy', 'PX_BID', '20180509','20180509')
except ValueError:
    print("ERROR")

ERROR