janlukasschroeder/sec-api-python

TypeError when raising exception

seasonedfish opened this issue · 1 comments

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.

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