Skip to content

Commit

Permalink
Skip formatting response when None
Browse files Browse the repository at this point in the history
  • Loading branch information
reedsa committed Jun 19, 2023
1 parent 3fe1e47 commit 1dcc261
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 18 additions & 1 deletion tests/core/middleware/test_formatting_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ def test_formatting_middleware_result_formatters(w3):
assert actual == expected


def test_formatting_middleware_result_formatters_for_none(w3):
w3.middleware_onion.add(
construct_result_generator_middleware(
{RPCEndpoint("test_endpoint"): lambda method, params: None}
)
)
w3.middleware_onion.add(
construct_formatting_middleware(
result_formatters={RPCEndpoint("test_endpoint"): lambda x: hex(x)}
)
)

expected = None
actual = w3.manager.request_blocking("test_endpoint", [])
assert actual == expected


def test_formatting_middleware_error_formatters(w3):
w3.middleware_onion.add(
construct_error_generator_middleware(
Expand All @@ -106,4 +123,4 @@ def test_formatting_middleware_error_formatters(w3):
expected = "error"
with pytest.raises(ValueError) as err:
w3.manager.request_blocking("test_endpoint", [])
assert str(err.value) == expected
assert str(err.value) == expected
6 changes: 5 additions & 1 deletion web3/middleware/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def _format_response(
response, response_type, method_response_formatter(appropriate_response)
)

if "result" in response and method in result_formatters:
if (
"result" in response
and response["result"] is not None
and method in result_formatters
):
return _format_response("result", result_formatters[method])
elif "error" in response and method in error_formatters:
return _format_response("error", error_formatters[method])
Expand Down

0 comments on commit 1dcc261

Please sign in to comment.