Skip to content

Commit f63670e

Browse files
committed
format error messages added it
1 parent 663bc36 commit f63670e

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

functions/report/libs/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@
22

33
def output(result):
44
status = 200 if result.success() else 400
5-
payload = result.result if result.success() else result.errors
6-
return (json.dumps(payload), status)
5+
payload = result.result if result.success() else convert_to_hashes(result.errors)
6+
return (json.dumps(payload), status)
7+
8+
def convert_to_hashes(arr):
9+
hashes_arr = []
10+
for inner_arr in arr:
11+
hash_dict = {inner_arr[0]: inner_arr[1]}
12+
hashes_arr.append(hash_dict)
13+
return hashes_arr

tests/test_report/libs/test_utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ def test_output():
1010
output_result_success = output(result_success)
1111

1212
# Verify that the output has the correct HTTP status code and payload
13-
assert output_result_success[0] == 200
14-
assert json.loads(output_result_success[1]) == {"message": "Hello, world!"}
13+
assert output_result_success[1] == 200
14+
assert json.loads(output_result_success[0]) == {"message": "Hello, world!"}
1515

1616
# Create a mock result object with an error status
17-
result_error = Result(status="error", errors=["Invalid request"])
17+
result_error = Result(status="error", errors=[["param", "Invalid request"]])
1818

1919
# Call the output function with the mock result object
2020
output_result_error = output(result_error)
2121

2222
# Verify that the output has the correct HTTP status code and payload
23-
assert output_result_error[0] == 400
24-
assert json.loads(output_result_error[1]) == ["Invalid request"]
23+
assert output_result_error[1] == 400
24+
assert json.loads(output_result_error[0]) == [{"param": "Invalid request"}]
25+
26+
def test_convert_to_hashes():
27+
input_arr = [["geo", "missing geo parameters"], ["app", "missing geo parameters"]]
28+
expected_output_arr = [{'geo': 'missing geo parameters'}, {'app': 'missing geo parameters'}]
29+
assert convert_to_hashes(input_arr) == expected_output_arr

0 commit comments

Comments
 (0)