Skip to content

Commit

Permalink
Adds custom response_error_message method (#12)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Hassan Syyid <[email protected]>
  • Loading branch information
vmesel and hsyyid authored Feb 2, 2024
1 parent 573c28e commit 11dbc78
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tap_stripe/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Dict, Iterable, Optional, cast

import requests
from requests.exceptions import JSONDecodeError
from memoization import cached
from backports.cached_property import cached_property
from singer_sdk.authenticators import BearerTokenAuthenticator
Expand Down Expand Up @@ -200,7 +201,37 @@ def request_decorator(self, func: Callable) -> Callable:
factor=2,
)(func)
return decorator


def response_error_message(self, response: requests.Response) -> str:
"""Build error message for invalid http statuses.
Args:
response: A `requests.Response`_ object.
Returns:
str: The error message
"""
if 400 <= response.status_code < 500:
error_type = "Client"
else:
error_type = "Server"

try:
response_content = response.json()

if response_content.get("error"):
error = response_content.get("error")
return f'Error: {error.get("message")} at path {self.path}'
except JSONDecodeError:
# ignore JSON errors
pass

return (
f"{response.status_code} {error_type} Error: "
f"{response.text} for path: {self.path}"
)

def validate_response(self, response: requests.Response) -> None:
if (
response.status_code in self.extra_retry_statuses
Expand All @@ -209,7 +240,7 @@ def validate_response(self, response: requests.Response) -> None:
msg = self.response_error_message(response)
raise RetriableAPIError(msg, response)
elif 400 <= response.status_code < 500 and response.status_code not in self.ignore_statuscode:
msg = response.text
msg = self.response_error_message(response)
raise FatalAPIError(msg)

def _write_state_message(self) -> None:
Expand Down

0 comments on commit 11dbc78

Please sign in to comment.