Skip to content

Commit

Permalink
Handle developer message in preview error responses (#971)
Browse files Browse the repository at this point in the history
* Handle developer_message in errors

* refactor

* fmt
  • Loading branch information
anniel-stripe authored May 25, 2023
1 parent 848a236 commit 60ee2b5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
40 changes: 32 additions & 8 deletions stripe/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ def handle_error_response(self, rbody, rcode, resp, rheaders):
raise err

def specific_api_error(self, rbody, rcode, resp, rheaders, error_data):
message = error_data.get("message") or error_data.get(
"developer_message"
)

util.log_info(
"Stripe API error received",
error_code=error_data.get("code"),
error_type=error_data.get("type"),
error_message=error_data.get("message"),
error_message=message,
error_param=error_data.get("param"),
)

Expand All @@ -190,16 +194,24 @@ def specific_api_error(self, rbody, rcode, resp, rheaders, error_data):
rcode == 400 and error_data.get("code") == "rate_limit"
):
return error.RateLimitError(
error_data.get("message"), rbody, rcode, resp, rheaders
message,
rbody,
rcode,
resp,
rheaders,
)
elif rcode in [400, 404]:
if error_data.get("type") == "idempotency_error":
return error.IdempotencyError(
error_data.get("message"), rbody, rcode, resp, rheaders
message,
rbody,
rcode,
resp,
rheaders,
)
else:
return error.InvalidRequestError(
error_data.get("message"),
message,
error_data.get("param"),
error_data.get("code"),
rbody,
Expand All @@ -209,11 +221,15 @@ def specific_api_error(self, rbody, rcode, resp, rheaders, error_data):
)
elif rcode == 401:
return error.AuthenticationError(
error_data.get("message"), rbody, rcode, resp, rheaders
message,
rbody,
rcode,
resp,
rheaders,
)
elif rcode == 402:
return error.CardError(
error_data.get("message"),
message,
error_data.get("param"),
error_data.get("code"),
rbody,
Expand All @@ -223,11 +239,19 @@ def specific_api_error(self, rbody, rcode, resp, rheaders, error_data):
)
elif rcode == 403:
return error.PermissionError(
error_data.get("message"), rbody, rcode, resp, rheaders
message,
rbody,
rcode,
resp,
rheaders,
)
else:
return error.APIError(
error_data.get("message"), rbody, rcode, resp, rheaders
message,
rbody,
rcode,
resp,
rheaders,
)

def specific_oauth_error(self, rbody, rcode, resp, rheaders, error_code):
Expand Down
8 changes: 6 additions & 2 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,12 @@ def test_extract_error_from_stream_request_for_response(
400,
)

with pytest.raises(stripe.oauth_error.InvalidGrantError):
requestor.request_stream("get", self.valid_path, {})
def test_developer_message_in_error(self, requestor, mock_response):
mock_response('{"error": {"developer_message": "Unacceptable"}}', 400)

with pytest.raises(stripe.error.InvalidRequestError) as excinfo:
requestor.request("get", self.valid_path, {})
assert excinfo.value.user_message == "Unacceptable"

def test_raw_request_with_file_param(self, requestor, mock_response):
test_file = tempfile.NamedTemporaryFile()
Expand Down

0 comments on commit 60ee2b5

Please sign in to comment.