From 813626a9add1b166a760c73ecbf3ef3bb44e5f65 Mon Sep 17 00:00:00 2001 From: Michael Helmick Date: Mon, 9 Apr 2012 10:59:13 -0400 Subject: [PATCH] Maybe the twitter_http_status_codes were a good idea. :P I still think it's weird to have them, but I'm not against giving the user more information. I put back in the twitter_http_status_codes variable, but I changed where the logic was being handled, instead of it happening the in _request, it will be asserted in Twython error if an error_code is passed AND the error_code is in twitter_http_status_codes --- twython/twitter_endpoints.py | 15 +++++++++++++++ twython/twython.py | 9 ++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/twython/twitter_endpoints.py b/twython/twitter_endpoints.py index cf4690b..85da63a 100644 --- a/twython/twitter_endpoints.py +++ b/twython/twitter_endpoints.py @@ -328,3 +328,18 @@ api_table = { 'method': 'POST', }, } + +# from https://dev.twitter.com/docs/error-codes-responses +twitter_http_status_codes = { + 200: ('OK', 'Success!'), + 304: ('Not Modified', 'There was no new data to return.'), + 400: ('Bad Request', 'The request was invalid. An accompanying error message will explain why. This is the status code will be returned during rate limiting.'), + 401: ('Unauthorized', 'Authentication credentials were missing or incorrect.'), + 403: ('Forbidden', 'The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits.'), + 404: ('Not Found', 'The URI requested is invalid or the resource requested, such as a user, does not exists.'), + 406: ('Not Acceptable', 'Returned by the Search API when an invalid format is specified in the request.'), + 420: ('Enhance Your Calm', 'Returned by the Search and Trends API when you are being rate limited.'), + 500: ('Internal Server Error', 'Something is broken. Please post to the group so the Twitter team can investigate.'), + 502: ('Bad Gateway', 'Twitter is down or being upgraded.'), + 503: ('Service Unavailable', 'The Twitter servers are up, but overloaded with requests. Try again later.'), +} diff --git a/twython/twython.py b/twython/twython.py index 1e3a1b4..c4b425c 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -27,7 +27,7 @@ except ImportError: # Twython maps keyword based arguments to Twitter API endpoints. The endpoints # table is a file with a dictionary of every API endpoint that Twython supports. -from twitter_endpoints import base_url, api_table +from twitter_endpoints import base_url, api_table, twitter_http_status_codes # There are some special setups (like, oh, a Django application) where @@ -64,8 +64,11 @@ class TwythonError(AttributeError): def __init__(self, msg, error_code=None): self.msg = msg - if error_code is not None: - self.msg = self.msg + ' Please see https://dev.twitter.com/docs/error-codes-responses for additional information.' + if error_code is not None and error_code in twitter_http_status_codes: + self.msg = '%s: %s -- %s' % \ + (twitter_http_status_codes[error_code][0], + twitter_http_status_codes[error_code][1], + self.msg) if error_code == 400 or error_code == 420: raise TwythonAPILimit(self.msg)