Skip to content

[V6] web3exception argument for user_message #3282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3282.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``user_message`` kwarg for human readable ``Web3Exception`` messages.
32 changes: 32 additions & 0 deletions tests/core/exceptions/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

from web3.exceptions import (
Web3Exception,
)


def test_web3exception_with_user_message():
with pytest.raises(Web3Exception) as exception:
raise Web3Exception(user_message="This failed!")
assert exception.type is Web3Exception
assert exception.value.user_message == "This failed!"


def test_web3exception_with_kwargs():
with pytest.raises(TypeError) as exception:
raise Web3Exception(data={"message": "Unable to fulfill your request."})

# For Python > 3.9, str exception includes 'Web3Exception.'
expected = "__init__() got an unexpected keyword argument 'data'"
actual = str(exception.value)
assert exception.type is TypeError
assert hasattr(exception.value, "data") is False
assert expected in actual


def test_web3exception_with_args():
with pytest.raises(Web3Exception) as exception:
raise Web3Exception("failed")
assert exception.type is Web3Exception
assert exception.value.user_message is None
assert exception.value.args[0] == "failed"
11 changes: 11 additions & 0 deletions web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class Web3Exception(Exception):
# deal with other exceptions
"""

def __init__(
self,
*args: Any,
user_message: Optional[str] = None,
):
super().__init__(*args)

# Assign properties of Web3Exception
self.user_message = user_message


class BadFunctionCallOutput(Web3Exception):
"""
Expand Down Expand Up @@ -274,6 +284,7 @@ def __init__(
message: Optional[str] = None,
data: Optional[Union[str, Dict[str, str]]] = None,
):
super().__init__(message, data)
self.message = message
self.data = data

Expand Down
6 changes: 5 additions & 1 deletion web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ def formatted_response(
if not isinstance(code, int):
_raise_bad_response_format(response, "error['code'] must be an integer")
elif code == METHOD_NOT_FOUND:
raise MethodUnavailable(error)
raise MethodUnavailable(
error,
user_message="Check your node provider's API docs to see what "
"methods are supported",
)

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