libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [libcloud] 11/14: Also correctly catch hostname not known error when sending non constumer key requests to the Ovh API and re-throw more user-friendly exceptio.
Date Sun, 04 Oct 2020 15:39:30 GMT
This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git

commit fcb937c2412910bf13fb1927620e80548a65712f
Author: Tomaz Muraus <tomaz@tomaz.me>
AuthorDate: Sun Oct 4 17:21:31 2020 +0200

    Also correctly catch hostname not known error when sending non constumer
    key requests to the Ovh API and re-throw more user-friendly exceptio.
---
 libcloud/common/ovh.py | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/libcloud/common/ovh.py b/libcloud/common/ovh.py
index 7d11917..5dd0687 100644
--- a/libcloud/common/ovh.py
+++ b/libcloud/common/ovh.py
@@ -138,13 +138,7 @@ class OvhConnection(ConnectionUserAndKey):
         try:
             httpcon.request(method='POST', url=action, body=data, headers=headers)
         except requests.exceptions.ConnectionError as e:
-            if 'name or service not known' in str(e).lower():
-                raise ValueError('Received "name or service not known" error '
-                                 'when requesting consumer key. This likely '
-                                 'indicates invalid region argument was'
-                                 'passed to the driver constructor.'
-                                 'Used host: %s. Original error: %s' %
-                                 (self.host, str(e)))
+            handle_and_rethrow_user_friendly_invalid_region_error(host=self.host, e=e)
 
         response = OvhResponse(httpcon.getresponse(), httpcon)
 
@@ -208,6 +202,29 @@ class OvhConnection(ConnectionUserAndKey):
             'X-Ovh-Timestamp': timestamp,
             'X-Ovh-Signature': signature
         })
-        return super(OvhConnection, self)\
-            .request(action, params=params, data=data, headers=headers,
-                     method=method, raw=raw)
+
+        try:
+            return super(OvhConnection, self)\
+                .request(action, params=params, data=data, headers=headers,
+                        method=method, raw=raw)
+        except requests.exceptions.ConnectionError as e:
+            handle_and_rethrow_user_friendly_invalid_region_error(host=self.host, e=e)
+
+
+def handle_and_rethrow_user_friendly_invalid_region_error(host, e):
+    """
+    Utility method which throws a more user-friendly error in case "name or
+    service not known" error is received when sending a request.
+
+    In most cases this error indicates user passed invalid ``region`` argument
+    to the driver constructor.
+    """
+    if 'name or service not known' in str(e).lower():
+        raise ValueError('Received "name or service not known" error '
+                         'when sending a request. This likely '
+                         'indicates invalid region argument was '
+                         'passed to the driver constructor.'
+                         'Used host: %s. Original error: %s' %
+                         (host, str(e)))
+
+    raise e


Mime
View raw message