-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
web3/exceptions.py
Outdated
""" | ||
|
||
|
||
class BadFunctionCallOutput(Exception, Web3Exception): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd advocate for Web3Exception
to inherit from Exception
and to keep the inheritance chain for all of these child exception classes to only have a single parent of Web3Exception
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for review and permission to break BC, notes added in new v6 migration document.
web3/exceptions.py
Outdated
""" | ||
Raised when an ABI is present, but doesn't contain any functions. | ||
""" | ||
pass | ||
|
||
|
||
class NoABIFound(AttributeError): | ||
class NoABIFound(AttributeError, Web3Exception): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kclowes can you make a note in your breaking v6 changes tracking issue to remove AttributeError
from these exception classes bases.
web3/exceptions.py
Outdated
""" | ||
Raised when there is no Infura Project Id set. | ||
""" | ||
pass | ||
|
||
|
||
class LogTopicError(ValueError): | ||
class LogTopicError(ValueError, Web3Exception): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kclowes same here.
6fa55e9
to
8a396f7
Compare
Added v6 migration notes |
8a396f7
to
0614a33
Compare
Thank you for this @jpic! We're finally about ready to pull it in. I just fixed a bunch of merge conflicts that have accumulated, and need to make some updates in the v6 migration guide, but then it looks good to go! |
0614a33
to
41a5919
Compare
4dd52c7
to
ff1158a
Compare
Exception mixin inherited by all exceptions of each module. This allows: try: some_call() except Web3Exception as e: # deal with web3 exception except: # deal with other exceptions
….PyEthPMException
ff1158a
to
b7ef938
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to see this making it in. I just need a bit of clarification on some things.
-
eth_utils
has aValidationError
that we use in a few places withinweb3.py
while in others we useweb3.exceptions.ValidationError
. It looks like at some point there was an idea to use theeth_utils.exceptions.ValidationError
across all of the python libraries. Indeed this isn't used anywhere within the actualeth-utils
library. Do we want to keep this design? Are there good arguments for or against it? This seems like a good time to revisit this forweb3.py
and it wouldn't be so hard of a change to make. If we think a better design is for each project to have their ownValidationError
class, do we want to go toward namespace validation errors, as we did withEthPMValidationError
? -
nit: I slightly prefer the less lengthy
EthPMException
overPyEthPMException
. ThePy
seems redundant and the module name isethpm
. But I can take or leave it. It does makeEthPMValidationError
seem like it needs aPy
if we keep it... but I prefer both without it.
edit: link to py-evm PR for this change. Also, note, this can (and probably should?) be a separate PR... just noticing that it is related and not on the list for v6
breaking change considerations.
I lean toward each project having their own
Yeah, agree the Py seems redundant. Will update! |
2709a33
to
3e77455
Compare
I personally don't see a scenario where I'd want to catch only web3 validation errors instead of catching them across all web3 libraries that However... if we want the most fine tuning, I don't see why we can't make Without a general from web3.exceptions import Web3ValidationError
from eth_tester.exceptions import EthTesterValidationError
from eth.exceptions import PyEVMValidationError
...
ALL_WEB3_VALIDATION_ERRORS = [
Web3ValidationError, EthTesterValidationError, PyEvmValidationError, ...,
]
try:
...
except Exception as e:
if isinstance(e, ALL_WEB3_VALIDATION_ERRORS):
... which looks a bit similar to the original issue filed for web3 exceptions in general. With a general from eth_utils.exceptions import ValidationError
try:
...
except ValidationError:
... For even more fine tuning, if we still opt for name-spaced validation errors which inherit from the general from eth_utils.exceptions import ValidationError
class Web3ValidationError(Web3Exception, ValidationError):
pass
try:
...
# catch all web3 exceptions, including Web3ValidationError
except Web3Exception:
...
# OR catch only web3 validation exceptions
except Web3ValidationError:
...
# OR catch any validation errors that may happen on calls to other web3 libraries
except ValidationError:
... My vote is either using |
Yeah, that's a good point. It feels a little like clunky to me to expect a user to import from |
Makes sense. We would only need to import |
ae7f5fe
to
6ed320b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 👍🏼
We can definitely benefit from some brief documentation on this hierarchy, perhaps in a separate PR?
…tils.exceptions.ValidationError
6ed320b
to
e348d68
Compare
Good thought! I'll add it to the migration guide PR that was created off of this branch. As soon as this gets in, I'll PR that. |
Exception mixin inherited by all exceptions of web3.py
This allows:
What was wrong?
I currently have this code:
This has readability problem, and also not forward-compatible with new exceptions, and also will break if an exception is removed from web3 package.
How was it fixed?
With this patch, I could replace the above with:
This fixes readability and forward-compatibility.
Cute Animal Picture