Skip to content

Add Web3Exception to all exception classes #1478

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
Jan 13, 2023
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
6 changes: 3 additions & 3 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Each Contract Factory exposes the following methods.

.. py:classmethod:: Contract.constructor(*args, **kwargs).build_transaction(transaction=None)
:noindex:

Construct the contract deploy transaction bytecode data.

If the contract takes constructor parameters they should be provided as
Expand Down Expand Up @@ -435,7 +435,7 @@ and the arguments are ambiguous.
}
"""
# fast forward all the steps of compiling and deploying the contract.
>>> ambiguous_contract.functions.identity(1, True) # raises ValidationError
>>> ambiguous_contract.functions.identity(1, True) # raises Web3ValidationError

>>> identity_func = ambiguous_contract.get_function_by_signature('identity(uint256,bool)')
>>> identity_func(1, True)
Expand Down Expand Up @@ -654,7 +654,7 @@ Taking the following contract code as an example:
>>> array_contract.functions.setBytes2Value([b'a']).transact()
Traceback (most recent call last):
...
ValidationError:
Web3ValidationError:
Could not identify the intended function with name `setBytes2Value`


Expand Down
39 changes: 28 additions & 11 deletions ens/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
from eth_utils import (
ValidationError,
)
import idna


class AddressMismatch(ValueError):
class ENSException(Exception):
"""
Base class for all ENS Errors
"""

pass


class AddressMismatch(ENSException):
"""
In order to set up reverse resolution correctly, the ENS name should first
point to the address. This exception is raised if the name does
Expand All @@ -11,7 +22,7 @@ class AddressMismatch(ValueError):
pass


class InvalidName(idna.IDNAError):
class InvalidName(idna.IDNAError, ENSException):
"""
This exception is raised if the provided name does not meet
the syntax standards specified in `EIP 137 name syntax
Expand All @@ -23,7 +34,7 @@ class InvalidName(idna.IDNAError):
pass


class UnauthorizedError(Exception):
class UnauthorizedError(ENSException):
"""
Raised if the sending account is not the owner of the name
you are trying to modify. Make sure to set ``from`` in the
Expand All @@ -33,7 +44,7 @@ class UnauthorizedError(Exception):
pass


class UnownedName(Exception):
class UnownedName(ENSException):
"""
Raised if you are trying to modify a name that no one owns.

Expand All @@ -44,47 +55,47 @@ class UnownedName(Exception):
pass


class ResolverNotFound(Exception):
class ResolverNotFound(ENSException):
"""
Raised if no resolver was found for the name you are trying to resolve.
"""

pass


class UnsupportedFunction(Exception):
class UnsupportedFunction(ENSException):
"""
Raised if a resolver does not support a particular method.
"""

pass


class BidTooLow(ValueError):
class BidTooLow(ENSException):
"""
Raised if you bid less than the minimum amount
"""

pass


class InvalidBidHash(ValueError):
class InvalidBidHash(ENSException):
"""
Raised if you supply incorrect data to generate the bid hash.
"""

pass


class InvalidLabel(ValueError):
class InvalidLabel(ENSException):
"""
Raised if you supply an invalid label
"""

pass


class OversizeTransaction(ValueError):
class OversizeTransaction(ENSException):
"""
Raised if a transaction you are trying to create would cost so
much gas that it could not fit in a block.
Expand All @@ -95,10 +106,16 @@ class OversizeTransaction(ValueError):
pass


class UnderfundedBid(ValueError):
class UnderfundedBid(ENSException):
"""
Raised if you send less wei with your bid than you declared
as your intent to bid.
"""

pass


class ENSValidationError(ENSException, ValidationError):
"""
Raised if there is a validation error
"""
6 changes: 4 additions & 2 deletions ens/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
HexStr,
)
from eth_utils import (
ValidationError,
is_same_address,
remove_0x_prefix,
to_bytes,
Expand All @@ -47,6 +46,7 @@
REVERSE_REGISTRAR_DOMAIN,
)
from ens.exceptions import (
ENSValidationError,
InvalidName,
)

Expand Down Expand Up @@ -142,7 +142,9 @@ def ens_encode_name(name: str) -> bytes:
# raises if len(label) > 255:
for index, label in enumerate(labels):
if len(label) > 255:
raise ValidationError(f"Label at position {index} too long after encoding.")
raise ENSValidationError(
f"Label at position {index} too long after encoding."
)

# concat label size in bytes to each label:
dns_prepped_labels = [to_bytes(len(label)) + label for label in labels_as_bytes]
Expand Down
27 changes: 20 additions & 7 deletions ethpm/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
class PyEthPMError(Exception):
from eth_utils import (
ValidationError,
)


class EthPMException(Exception):
"""
Base class for all Py-EthPM errors.
"""

pass


class InsufficientAssetsError(PyEthPMError):
class InsufficientAssetsError(EthPMException):
"""
Raised when a Manifest or Package does not contain the required
assets to do something.
Expand All @@ -15,41 +20,49 @@ class InsufficientAssetsError(PyEthPMError):
pass


class EthPMValidationError(PyEthPMError):
class EthPMValidationError(EthPMException, ValidationError):
"""
Raised when something does not pass a validation check.
"""

pass


class CannotHandleURI(PyEthPMError):
class CannotHandleURI(EthPMException):
"""
Raised when the given URI cannot be served by any of the available backends.
"""

pass


class FailureToFetchIPFSAssetsError(PyEthPMError):
class FailureToFetchIPFSAssetsError(EthPMException):
"""
Raised when an attempt to fetch a Package's assets via IPFS failed.
"""

pass


class BytecodeLinkingError(PyEthPMError):
class BytecodeLinkingError(EthPMException):
"""
Raised when an attempt to link a contract factory's bytecode failed.
"""

pass


class ManifestBuildingError(PyEthPMError):
class ManifestBuildingError(EthPMException):
"""
Raised when an attempt to build a manifest failed.
"""

pass


class ManifestValidationError(EthPMException):
"""
Raised when a provided manifest cannot be published, since it's invalid.
"""

pass
4 changes: 2 additions & 2 deletions ethpm/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
)
from ethpm.exceptions import (
BytecodeLinkingError,
EthPMException,
EthPMValidationError,
FailureToFetchIPFSAssetsError,
InsufficientAssetsError,
PyEthPMError,
)
from ethpm.uri import (
resolve_uri_contents,
Expand Down Expand Up @@ -338,7 +338,7 @@ def build_dependencies(self) -> "Dependencies":
try:
validate_build_dependency(name, uri)
dependency_package = Package.from_uri(uri, self.w3)
except PyEthPMError as e:
except EthPMException as e:
raise FailureToFetchIPFSAssetsError(
f"Failed to retrieve build dependency: {name} from URI: {uri}.\n"
f"Got error: {e}."
Expand Down
1 change: 1 addition & 0 deletions newsfragments/1478.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All exceptions inherit from a custom class. EthPM exceptions inherit from EthPMException, ENS exceptions inherit from ENSException, and all other web3.py exceptions inherit from Web3Exception
6 changes: 3 additions & 3 deletions tests/core/contracts/test_contract_build_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
)

from web3.exceptions import (
ValidationError,
Web3ValidationError,
)


Expand All @@ -28,7 +28,7 @@ def test_build_transaction_not_paying_to_nonpayable_function(
def test_build_transaction_paying_to_nonpayable_function(
w3, payable_tester_contract, build_transaction
):
with pytest.raises(ValidationError):
with pytest.raises(Web3ValidationError):
build_transaction(
contract=payable_tester_contract,
contract_function="doNoValueCall",
Expand Down Expand Up @@ -277,7 +277,7 @@ async def test_async_build_transaction_not_paying_to_nonpayable_function(
async def test_async_build_transaction_paying_to_nonpayable_function(
async_w3, async_payable_tester_contract, async_build_transaction
):
with pytest.raises(ValidationError):
with pytest.raises(Web3ValidationError):
await async_build_transaction(
contract=async_payable_tester_contract,
contract_function="doNoValueCall",
Expand Down
Loading