Skip to content

Commit

Permalink
Merge pull request #87 from mwarzybok-sumoheavy/feature/SP-874
Browse files Browse the repository at this point in the history
SP-874 Support "errors" array
  • Loading branch information
bobbrodie authored Feb 26, 2024
2 parents 2454ebf + 1d366b0 commit 80d4866
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/bitpay/clients/response_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ def response_to_json_string(response: Response) -> Any:
response_obj["error"]
)

if "errors" in response_obj:
final_message = ""
for error in response_obj["errors"]:
error_value = error.get("error", "")
param_value = error.get("param", "")

if error_value.endswith("."):
error_value = error_value[:-1]

if error_value:
result = f"{error_value} {param_value}".strip()
else:
result = param_value.strip()

if not result.endswith("."):
result += "."

if not final_message == "":
result = " " + result

final_message += result
BitPayExceptionProvider.throw_api_exception_with_message(final_message)

if "success" in response_obj:
return response_obj["success"]

Expand Down
1 change: 1 addition & 0 deletions tests/unit/clients/response_with_errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"errors":[{"error":"Missing required parameter.","param":"price"},{"error":"Missing required parameter.","param":"currency"}]}
29 changes: 29 additions & 0 deletions tests/unit/clients/test_response_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import os

import pytest

from requests import Response, PreparedRequest

from bitpay.clients.response_parser import ResponseParser
from bitpay.exceptions.bitpay_api_exception import BitPayApiException


@pytest.mark.unit
def test_handle_multiple_errors(mocker):
with pytest.raises(BitPayApiException) as exc_info:
with open(
os.path.abspath(os.path.dirname(__file__))
+ "/response_with_errors.json",
"r",
) as file:
response_json = json.load(file)
request = mocker.Mock(spec=PreparedRequest)
request.method = "POST"
request.url = "https://some-url.com"
response = mocker.Mock(spec=Response)
response.request = request
response.json.return_value = response_json
ResponseParser.response_to_json_string(response)

assert str(exc_info.value) == "Missing required parameter price. Missing required parameter currency."

0 comments on commit 80d4866

Please sign in to comment.