Skip to content

Commit

Permalink
better exeptions for 500 and 400
Browse files Browse the repository at this point in the history
fix #130

Signed-off-by: Mikhaël LOLLO <mikhael.lollo@3ds.com>
Signed-off-by: Matthias Gatto <matthias.gatto@outscale.com>
  • Loading branch information
outscale-mgo committed May 16, 2024
1 parent d0a7cd2 commit ebbc0ba
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion osc_sdk_python/osc-api
Submodule osc-api updated 3 files
+117 −124 old-outscale.yaml
+320 −1,716 outscale-java.yaml
+329 −1,719 outscale.yaml
80 changes: 79 additions & 1 deletion osc_sdk_python/requester.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from requests import Session, HTTPError
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from json import JSONDecodeError

class Requester:
def __init__(self, auth, endpoint, max_retries=0, backoff_factor=0.5, status_forcelist=[400, 500]):
Expand All @@ -23,5 +25,81 @@ def send(self, uri, payload):
session.mount("https://", self.adapter)
session.mount("http://", self.adapter)
response = session.post(self.endpoint, data=payload, headers=headers, verify=True,)
response.raise_for_status()
self.raise_for_status(response)
return response.json()

def raise_for_status(self, response):
http_error_msg = ""
error_code = None
request_id = None
reason = self.get_default_reason(response)

try:
error = response.json()
except JSONDecodeError:
pass
else:
if "__type" in error:
error_code = error.get("__type")
reason = error.get("message")
request_id = response.headers.get("x-amz-requestid")
else:
request_id = (error.get("ResponseContext") or {}).get("RequestId")
errors = error.get("Errors")
if errors:
error = errors[0]
error_code = error.get("Code")
reason = error.get("Type")
if error.get("Details"):
code_type = reason
reason = error.get("Details")
else:
code_type = None

if 400 <= response.status_code < 500:
if error_code and request_id:
http_error_msg = (
f"Client Error --> status = {response.status_code}, "
f"code = {error_code}, "
f'{"code_type = " if code_type is not None else ""}'
f'{code_type + ", " if code_type is not None else ""}'
f"Reason = {reason}, "
f"request_id = {request_id}, "
f"url = {response.url}"
)
else:
http_error_msg = (
f"{response.status_code} Client Error: {reason} for url: {response.url}"
)

elif 500 <= response.status_code < 600:
if error_code and request_id:
http_error_msg = (
f"Server Error --> status = {response.status_code}, "
f"code = {error_code}, "
f'{"code_type = " if code_type is not None else ""}'
f'{code_type + ", " if code_type is not None else ""}'
f"Reason = {reason}, "
f"request_id = {request_id}, "
f"url = {response.url}"
)
else:
http_error_msg = (
f"{response.status_code} Server Error: {reason} for url: {response.url}"
)

if http_error_msg:
raise HTTPError(http_error_msg, response=response)

def get_default_reason(self, response):
if isinstance(response.reason, bytes):
# We attempt to decode utf-8 first because some servers
# choose to localize their reason strings. If the string
# isn't utf-8, we fall back to iso-8859-1 for all other
# encodings. (See PR #3538)
try:
return response.reason.decode("utf-8")
except UnicodeDecodeError:
return response.reason.decode("iso-8859-1")
else:
return response.reason

0 comments on commit ebbc0ba

Please sign in to comment.