-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add API to ABI coder for error recovery #800
Comments
I've added initial support for option 2 above. Basically, if you have a signature like Imagine that const utils = ethers.utils;
// This works fine:
console.log(result.bar);
// This would throw:
console.log(result.foo);
// However, there error contains sufficient data to recover:
try {
console.log(result.foo);
} catch (error) {
const de = error.decoderError;
// console.log(de);
// Error: "phrase: invalid codepoint at offset 0; bad codepoint prefix (argument="bytes", value={"0":255,"1":101,"2":108,"3":108,"4":111,"5":32,"6":87,"7":111,"8":114,"9":108,"10":100}, code=INVALID_ARGUMENT, version=strings/5.0.0-beta.136)" {
// reason: 'invalid codepoint at offset 0; bad codepoint prefix',
// code: 'INVALID_ARGUMENT',
// argument: 'bytes',
// value: Uint8Array [ 255, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 ],
// baseType: 'string',
// name: 'phrase',
// type: 'string'
// }
const replaced = utils.toUtf8String(de.value, utils.Utf8ErrorFuncs.ignore);
console.log(replaced);
} So, non-exceptional cases can work like normal, and if someone cares about badly formatted data, they can detect it and correct it. Thoughts? |
(oh also, if there is bad data, but in a part that the person doesn't care about, they never even need to know) |
Some quick notes on trying to adopt this. Framework developers that wish to process errors in a specific way may need some introspection, especially if Consider the following: // Simulate a Result object with a bad item at index 2
a = [ 1, 2, 3 ]
Object.defineProperty(a, 2, { get: () => { throw new Error("foo"); } })
// [ 1, 2, [Getter] ]
//////////
// Safe
Object.keys(a)
// [ '0', '1', '2' ]
function foo() { }
foo.call(null, a)
a[1]
for (let i = 0; i < a.length; i++) { console.log(i); }
//////////
// Unsafe (anything that requires dereferencing the bad value)
Object.values(a)
// Including apply
function foo() { }
foo.apply(null, a)
a[2]
for (let i = 0; i < a.length; i++) { console.log(a[i]); } |
This has been added. :) |
@ricmoo I'm still facing this issue with attempts to return string to js. function hashCollaborators(Collaborator[] memory collaborators) public view returns(string memory) {
bytes32 digest = keccak256(abi.encode(collaborators));
return string(abi.encode(digest));
} And your approach with try-catch |
When decoding events and functions for an ABI, various errors can occur, which are currently swallowed.
It would be nice if there was a way to gracefully recover from errors.
Errors to consider:
There is still some effort to figure out the best way to bubble these up to higher level libraries, but adding some of the functionality to the low-level coder should be fairly straight forward.
Possible Solutions:
Result
object that will throw when accessed, but with an error that contains the problematic dataI'll be experimenting, and please feel free to make additional suggestions. :)
The text was updated successfully, but these errors were encountered: