question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add API to ABI coder for error recovery

See original GitHub issue

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:

  • Invalid UTF-8 string data (this is the most common); if a
  • Insufficient data; this can occur when there is a mismatch in which event arguments are indexed or a function return type is incorrect

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:

  1. Allow an errorFunc function which will be called on errors to provide an alternate value
  2. Place a getter on the Result object that will throw when accessed, but with an error that contains the problematic data
  3. Throw an error, with a recover function which will allow continuing decoding after the state has been corrected

I’ll be experimenting, and please feel free to make additional suggestions. 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ricmoocommented, Apr 25, 2020

I’ve added initial support for option 2 above.

Basically, if you have a signature like "event something(string bar, uint bar)", you willet back a Result object.

Imagine that bar has overlong code units in it and decode the Result object:

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?

0reactions
progsmilecommented, Jan 28, 2022

@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 ethers.utils.toUtf8String(error.value, ethers.utils.Utf8ErrorFuncs.ignore) works! But, is there better solution? Thanks in advance!

Read more comments on GitHub >

github_iconTop Results From Across the Web

AbiCoder - Ethers.js
The AbiCoder is a collection of Coders which can be used to encode and decode the binary data formats used to interoperate between...
Read more >
FIDL binary-compatibility (ABI) and source-compatibility (API ...
Often, a source-compatible rename is possible following the long process of adding a duplicate member with the desired name, switching all code ......
Read more >
What is an application binary interface (ABI)? - Stack Overflow
The API consists of data types/structures, constants, functions, etc that you can use in your code to access the functionality of that external...
Read more >
ABI Stability | Android Open Source Project
Application Binary Interface (ABI) stability is a prerequisite of framework-only updates because vendor modules may depend on the Vendor ...
Read more >
28. ABI and API Deprecation - Documentation
rte_atomicNN_xxx: These APIs do not take memory order parameter. This does not allow for writing optimized code for all the CPU architectures supported...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found