janlukasschroeder / sec-api-python

Python SEC EDGAR Filings API. Over 18 million filings, all 150 filing types supported. Query, full-text search and real-time stream API. Convert XBRL-to-JSON and access standardized financial statements from 10-K and 10-Q filings.
https://sec-api.io
MIT License
181 stars 24 forks source link

TypeError when raising exception #5

Closed seasonedfish closed 2 years ago

seasonedfish commented 2 years ago

When sec_api tries to raise an exception, it causes a TypeError:

Traceback (most recent call last):
  File "/Users/fisher/PycharmProjects/s1extract/s1extract/api/sec_api_download.py", line 68, in <module>
    main()
  File "/Users/fisher/PycharmProjects/s1extract/s1extract/api/sec_api_download.py", line 64, in main
    download_s1_html(firm.ticker_symbol)
  File "/Users/fisher/PycharmProjects/s1extract/s1extract/api/sec_api_download.py", line 51, in download_s1_html
    html_string = RENDER_API.get_filing(url)
  File "/Users/fisher/PycharmProjects/s1extract/.venv/lib/python3.9/site-packages/sec_api/index.py", line 86, in get_filing
    raise Exception("API error: " + response.status_code)
TypeError: can only concatenate str (not "int") to str

Since response.status_code is an int, it cannot be concatenated to the preceding string. This can be fixed by casting response.status_code to a string.

seasonedfish commented 2 years ago

Hmm, another option is to define a new exception APIError, that way applications calling sec-api can catch that specific one instead of Exception. (From stack overflow)

This also simplifies the code, because you can write it like this:

@@ -30,10 +34,10 @@ class QueryApi:
                 # wait 500 * (x + 1) milliseconds and try again
                 time.sleep(0.5 * (x + 1))
             else:
-                raise Exception("API error: " + str(response.status_code))
+                raise APIError(response.status_code)
         else:
             # request failed
-            raise Exception("API error")
+            raise APIError