You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by qu...@apache.org on 2017/11/10 04:29:32 UTC

[2/3] libcloud git commit: RateLimitReachedError exception sets message, code and headers.

RateLimitReachedError exception sets message, code and headers.

Added docstring documentation describing HTTP-date format
translation on Retry-After header.

Signed-off-by: Quentin Pradet <qu...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/dfebc6fc
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/dfebc6fc
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/dfebc6fc

Branch: refs/heads/trunk
Commit: dfebc6fc1b2709c0286ff6f6b449e3f90124f785
Parents: bd32dfc
Author: Lucas Di Pentima <ld...@veritasgenetics.com>
Authored: Fri Nov 3 18:31:29 2017 -0300
Committer: Quentin Pradet <qu...@apache.org>
Committed: Fri Nov 10 08:24:56 2017 +0400

----------------------------------------------------------------------
 libcloud/common/exceptions.py | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/dfebc6fc/libcloud/common/exceptions.py
----------------------------------------------------------------------
diff --git a/libcloud/common/exceptions.py b/libcloud/common/exceptions.py
index af08bf8..e9f12b5 100644
--- a/libcloud/common/exceptions.py
+++ b/libcloud/common/exceptions.py
@@ -51,7 +51,14 @@ class RateLimitReachedError(BaseHTTPError):
     message = '%s Rate limit exceeded' % (code)
 
     def __init__(self, *args, **kwargs):
-        self.retry_after = int(kwargs.pop('headers', {}).get('retry-after', 0))
+        headers = kwargs.pop('headers', None)
+        super(RateLimitReachedError, self).__init__(self.code,
+                                                    self.message,
+                                                    headers)
+        if self.headers is not None:
+            self.retry_after = int(self.headers.get('retry-after', 0))
+        else:
+            self.retry_after = 0
 
 
 _error_classes = [RateLimitReachedError]
@@ -62,9 +69,22 @@ def exception_from_message(code, message, headers=None):
     """
     Return an instance of BaseHTTPException or subclass based on response code.
 
+    If headers include Retry-After, RFC 2616 says that its value may be one of
+    two formats: HTTP-date or delta-seconds, for example:
+
+    Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
+    Retry-After: 120
+
+    If Retry-After comes in HTTP-date, it'll be translated to a positive
+    delta-seconds value when passing it to the exception constructor.
+
+    Also, RFC 2616 says that Retry-After isn't just only applicable to 429
+    HTTP status, but also to other responses, like 503 and 3xx.
+
     Usage::
         raise exception_from_message(code=self.status,
-                                     message=self.parse_error())
+                                     message=self.parse_error(),
+                                     headers=self.headers)
     """
     kwargs = {
         'code': code,