Skip to content

Commit 8f3b96e

Browse files
SP-874 Support "errors" array
1 parent 2454ebf commit 8f3b96e

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/bitpay/clients/response_parser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ def response_to_json_string(response: Response) -> Any:
3737
response_obj["error"]
3838
)
3939

40+
if "errors" in response_obj:
41+
message = ""
42+
for error in response_obj["errors"]:
43+
if message == "":
44+
message += str(error["error"])
45+
else:
46+
message += " " + str(error["error"])
47+
BitPayExceptionProvider.throw_api_exception_with_message(message)
48+
4049
if "success" in response_obj:
4150
return response_obj["success"]
4251

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"errors":[{"error":"Missing required parameter.","param":"price"},{"error":"Missing required parameter.","param":"currency"}]}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
import os
3+
4+
import pytest
5+
6+
from requests import Response, PreparedRequest
7+
8+
from bitpay.clients.response_parser import ResponseParser
9+
from bitpay.exceptions.bitpay_api_exception import BitPayApiException
10+
11+
12+
@pytest.mark.unit
13+
def test_handle_multiple_errors(mocker):
14+
with pytest.raises(BitPayApiException) as exc_info:
15+
with open(
16+
os.path.abspath(os.path.dirname(__file__))
17+
+ "/response_with_errors.json",
18+
"r",
19+
) as file:
20+
response_json = json.load(file)
21+
request = mocker.Mock(spec=PreparedRequest)
22+
request.method = "POST"
23+
request.url = "https://some-url.com"
24+
response = mocker.Mock(spec=Response)
25+
response.request = request
26+
response.json.return_value = response_json
27+
ResponseParser.response_to_json_string(response)
28+
29+
assert str(exc_info.value) == "Missing required parameter. Missing required parameter."

0 commit comments

Comments
 (0)