Skip to content

Commit

Permalink
Chain exceptions when reraising them (#1366)
Browse files Browse the repository at this point in the history
This makes debugging easier, since the original exception's details and traceback aren't just flattened to a string
  • Loading branch information
akx authored Aug 26, 2024
1 parent 5ff3d4b commit 86c9f23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions stripe/_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def _request_internal(

return content, status_code, result.headers

def _handle_request_error(self, e) -> NoReturn:
def _handle_request_error(self, e: Exception) -> NoReturn:
# Catch SSL error first as it belongs to ConnectionError,
# but we don't want to retry
if isinstance(e, self.requests.exceptions.SSLError):
Expand Down Expand Up @@ -764,7 +764,7 @@ def _handle_request_error(self, e) -> NoReturn:
should_retry = False

msg = textwrap.fill(msg) + "\n\n(Network error: %s)" % (err,)
raise APIConnectionError(msg, should_retry=should_retry)
raise APIConnectionError(msg, should_retry=should_retry) from e

def close(self):
if getattr(self._thread_local, "session", None) is not None:
Expand Down Expand Up @@ -869,7 +869,7 @@ def _request_internal(

return content, result.status_code, result.headers

def _handle_request_error(self, e, url) -> NoReturn:
def _handle_request_error(self, e: Exception, url: str) -> NoReturn:
if isinstance(e, self.urlfetch.InvalidURLError):
msg = (
"The Stripe library attempted to fetch an "
Expand All @@ -892,7 +892,7 @@ def _handle_request_error(self, e, url) -> NoReturn:
)

msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")"
raise APIConnectionError(msg)
raise APIConnectionError(msg) from e

def close(self):
pass
Expand Down Expand Up @@ -1046,7 +1046,7 @@ def _request_internal(

return rcontent, rcode, headers

def _handle_request_error(self, e) -> NoReturn:
def _handle_request_error(self, e: Exception) -> NoReturn:
if e.args[0] in [
self.pycurl.E_COULDNT_CONNECT,
self.pycurl.E_COULDNT_RESOLVE_HOST,
Expand Down Expand Up @@ -1079,7 +1079,7 @@ def _handle_request_error(self, e) -> NoReturn:
should_retry = False

msg = textwrap.fill(msg) + "\n\n(Network error: " + e.args[1] + ")"
raise APIConnectionError(msg, should_retry=should_retry)
raise APIConnectionError(msg, should_retry=should_retry) from e

def _get_proxy(self, url) -> Optional[ParseResult]:
if self._parsed_proxy:
Expand Down Expand Up @@ -1194,13 +1194,13 @@ def _request_internal(
lh = dict((k.lower(), v) for k, v in iter(dict(headers).items()))
return rcontent, rcode, lh

def _handle_request_error(self, e) -> NoReturn:
def _handle_request_error(self, e: Exception) -> NoReturn:
msg = (
"Unexpected error communicating with Stripe. "
"If this problem persists, let us know at support@stripe.com."
)
msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")"
raise APIConnectionError(msg)
raise APIConnectionError(msg) from e

def close(self):
pass
Expand Down Expand Up @@ -1307,7 +1307,7 @@ async def request_async(
response_headers = response.headers
return content, status_code, response_headers

def _handle_request_error(self, e) -> NoReturn:
def _handle_request_error(self, e: Exception) -> NoReturn:
msg = (
"Unexpected error communicating with Stripe. If this "
"problem persists, let us know at support@stripe.com."
Expand All @@ -1316,7 +1316,7 @@ def _handle_request_error(self, e) -> NoReturn:
should_retry = True

msg = textwrap.fill(msg) + "\n\n(Network error: %s)" % (err,)
raise APIConnectionError(msg, should_retry=should_retry)
raise APIConnectionError(msg, should_retry=should_retry) from e

def request_stream(
self, method: str, url: str, headers: Mapping[str, str], post_data=None
Expand Down Expand Up @@ -1446,7 +1446,7 @@ async def request_async(

return (await content.read()), status_code, response_headers

def _handle_request_error(self, e) -> NoReturn:
def _handle_request_error(self, e: Exception) -> NoReturn:
msg = (
"Unexpected error communicating with Stripe. If this "
"problem persists, let us know at support@stripe.com."
Expand All @@ -1455,7 +1455,7 @@ def _handle_request_error(self, e) -> NoReturn:
should_retry = True

msg = textwrap.fill(msg) + "\n\n(Network error: %s)" % (err,)
raise APIConnectionError(msg, should_retry=should_retry)
raise APIConnectionError(msg, should_retry=should_retry) from e

def request_stream(self) -> Tuple[Iterable[bytes], int, Mapping[str, str]]:
raise NotImplementedError(
Expand Down
2 changes: 1 addition & 1 deletion stripe/_stripe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __getattr__(self, k):
k = self._field_remappings[k]
return self[k]
except KeyError as err:
raise AttributeError(*err.args)
raise AttributeError(*err.args) from err

def __delattr__(self, k):
if k[0] == "_" or k in self.__dict__:
Expand Down

0 comments on commit 86c9f23

Please sign in to comment.