mikeshultz/ledger-eth-lib

Exception handling improvements

Closed this issue · 3 comments

The error experience when not properly connected to a device could be improved.

Another case would be to explore errors for not having the app installed and making APDU specific errors come out okay.

I spent some time on this trying to translate exceptions:

def translate_exception(exp: Exception) -> Exception:
"""Translate a CommException into something more useful to the user"""
string_err = str(exp)
if "No dongle found" in string_err:
return Exception("Ledger Nano not found")
elif "6804" in string_err:
return Exception("Ledger appears to be locked?")
elif any(x in string_err for x in ["6700", "6d00"]):
return Exception("Please open the Ethereum app on your Ledger device")
elif "6a80" in string_err:
return Exception(
"General failure: Invalid transaction, contract data not allowed in settings, or"
" something else?"
)
else:
return exp

But there are still a bunch of gaps, like when trying to init the dongle. For example:

ledgerblue.commException.CommException: Exception : No dongle found

It's still somewhat clear, but maybe all exceptions should be from this library and not its dependencies. Do you have any specific gripes you've run into?

I have noticed that if I don't have the Ethereum app open, it will also complain about not finding the dongle.
Here is how we are handling this in ape-ledger. It is not perfect but gives a few scenarios:

https://github.com/ApeWorX/ape-ledger/blob/main/ape_ledger/client.py#L173-L179

Here is a response verification we do:
https://github.com/ApeWorX/ape-ledger/blob/main/ape_ledger/client.py#L173-L179

Also here is our comms error mapping:
https://github.com/ApeWorX/ape-ledger/blob/main/ape_ledger/client.py#L68-L82

Timeout is also nice to have:
https://github.com/ApeWorX/ape-ledger/blob/main/ape_ledger/exceptions.py#L23

Also, when trying to sign an unknown message type:
https://github.com/ApeWorX/ape-ledger/blob/main/ape_ledger/accounts.py#L86

Used a bunch of your error defs and pulled a few from the app-ethereum source. Added some better exceptions and handling in PR #23. Since you raised this issue, I would appreciate your thoughts on the changes at your leisure.