Skip to content

Commit 3a6068f

Browse files
author
Aaron Gonzales
authored
Improve API error messaging (#27)
* passing error messages from the underlying API to the logger instead of using copy from the documentation * removing GNIP_RESPONSE_CODES as unneeded * updated version
1 parent ae1baa6 commit 3a6068f

File tree

3 files changed

+6
-38
lines changed

3 files changed

+6
-38
lines changed

searchtweets/api_utils.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,10 @@
1717

1818
__all__ = ["gen_rule_payload", "gen_params_from_config",
1919
"infer_endpoint", "convert_utc_time",
20-
"validate_count_api", "GNIP_RESP_CODES", "change_to_count_endpoint"]
20+
"validate_count_api", "change_to_count_endpoint"]
2121

2222
logger = logging.getLogger(__name__)
2323

24-
GNIP_RESP_CODES = {
25-
'200': ("OK: The request was successful. "
26-
"The JSON response will be similar to the following:"),
27-
28-
'400': ("Bad Request: Generally, this response occurs due "
29-
"to the presence of invalid JSON in the request, "
30-
"or where the request failed to send any JSON payload."),
31-
32-
'401': ("Unauthorized: HTTP authentication failed due to invalid "
33-
"credentials. Log in to console.gnip.com with your credentials "
34-
"to ensure you are using them correctly with your request. "),
35-
'404': ("Not Found: The resource was not found at the URL to which the "
36-
"request was sent, likely because an incorrect URL was used."),
37-
38-
'422': ("Unprocessable Entity: This is returned due to invalid parameters "
39-
"in a query or when a query is too complex for us to process. "
40-
"–e.g. invalid PowerTrack rules or too many phrase operators,"
41-
" rendering a query too complex."),
42-
'429': ("Unknown Code: Your app has exceeded the limit on connection "
43-
"requests. The corresponding JSON message will look "
44-
"similar to the following:"),
45-
'500': ("Internal Server Error: There was an error on Gnip's side. Retry "
46-
"your request using an exponential backoff pattern."),
47-
'502': ("Proxy Error: There was an error on Gnip's side. Retry your "
48-
"request using an exponential backoff pattern."),
49-
'503': ("Service Unavailable: There was an error on Gnip's side. "
50-
"Retry your request using an exponential backoff pattern.")
51-
}
52-
53-
5424
def convert_utc_time(datetime_str):
5525
"""
5626
Handles datetime argument conversion to the GNIP API format, which is

searchtweets/result_stream.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
from .utils import merge_dicts
2323

24-
from .api_utils import (infer_endpoint, GNIP_RESP_CODES,
25-
change_to_count_endpoint)
24+
from .api_utils import infer_endpoint, change_to_count_endpoint
2625

2726

2827
logger = logging.getLogger(__name__)
@@ -94,10 +93,9 @@ def retried_func(*args, **kwargs):
9493
break
9594

9695
if resp.status_code != 200:
97-
logger.error("HTTP Error code: {}: {}"
98-
.format(resp.status_code,
99-
GNIP_RESP_CODES[str(resp.status_code)]))
100-
logger.error("rule payload: {}".format(kwargs["rule_payload"]))
96+
error_message = resp.json()["error"]["message"]
97+
logger.error("HTTP Error code: {}: {}".format(resp.status_code, error_message))
98+
logger.error("Rule payload: {}".format(kwargs["rule_payload"]))
10199
raise requests.exceptions.HTTPError
102100

103101
return resp

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
long_description=open('README.rst', 'r', encoding="utf-8").read(),
88
author_email='agonzales@twitter.com',
99
license='MIT',
10-
version='1.3.2',
10+
version='1.4.0',
1111
python_requires='>=3.3',
1212
install_requires=["requests", "tweet_parser", "pyyaml"],
1313
packages=find_packages(),

0 commit comments

Comments
 (0)