Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle developer message in preview error responses #971

Merged
merged 3 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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