Skip to content

Commit 381f98c

Browse files
authored
[V6] web3exception argument for user_message (#3282)
* Add `user_message` to `Web3Exception` * Move block-utils tests to core * Tests for raising Web3Exception * Explicit assignment of `user_message` kwarg, disallow kwargs in general * Newsfragment for #3282
1 parent 459c0d0 commit 381f98c

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

newsfragments/3282.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``user_message`` kwarg for human readable ``Web3Exception`` messages.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
from web3.exceptions import (
4+
Web3Exception,
5+
)
6+
7+
8+
def test_web3exception_with_user_message():
9+
with pytest.raises(Web3Exception) as exception:
10+
raise Web3Exception(user_message="This failed!")
11+
assert exception.type is Web3Exception
12+
assert exception.value.user_message == "This failed!"
13+
14+
15+
def test_web3exception_with_kwargs():
16+
with pytest.raises(TypeError) as exception:
17+
raise Web3Exception(data={"message": "Unable to fulfill your request."})
18+
19+
# For Python > 3.9, str exception includes 'Web3Exception.'
20+
expected = "__init__() got an unexpected keyword argument 'data'"
21+
actual = str(exception.value)
22+
assert exception.type is TypeError
23+
assert hasattr(exception.value, "data") is False
24+
assert expected in actual
25+
26+
27+
def test_web3exception_with_args():
28+
with pytest.raises(Web3Exception) as exception:
29+
raise Web3Exception("failed")
30+
assert exception.type is Web3Exception
31+
assert exception.value.user_message is None
32+
assert exception.value.args[0] == "failed"

web3/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ class Web3Exception(Exception):
3030
# deal with other exceptions
3131
"""
3232

33+
def __init__(
34+
self,
35+
*args: Any,
36+
user_message: Optional[str] = None,
37+
):
38+
super().__init__(*args)
39+
40+
# Assign properties of Web3Exception
41+
self.user_message = user_message
42+
3343

3444
class BadFunctionCallOutput(Web3Exception):
3545
"""
@@ -274,6 +284,7 @@ def __init__(
274284
message: Optional[str] = None,
275285
data: Optional[Union[str, Dict[str, str]]] = None,
276286
):
287+
super().__init__(message, data)
277288
self.message = message
278289
self.data = data
279290

web3/manager.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ def formatted_response(
276276
if not isinstance(code, int):
277277
_raise_bad_response_format(response, "error['code'] must be an integer")
278278
elif code == METHOD_NOT_FOUND:
279-
raise MethodUnavailable(error)
279+
raise MethodUnavailable(
280+
error,
281+
user_message="Check your node provider's API docs to see what "
282+
"methods are supported",
283+
)
280284

281285
# Errors must include a message
282286
if not isinstance(error.get("message"), str):

0 commit comments

Comments
 (0)