fuels-ts icon indicating copy to clipboard operation
fuels-ts copied to clipboard

Supply better error messaging on Reverts

Open camsjams opened this issue 1 year ago • 3 comments

Motivation

A Sway program can define and throw custom error types that are also sent back to caller app (in this case the TS-SDK), it would be useful to generate and map these to custom error objects in JS

Usage example

Temporary basic premise

pub enum InputError {
    PriceCantBeZero: (),
}

impl SomeContract for Contract {
    fn validate_inputs(token_id: u64, price: u64) {
        require(price != 0, InputError::PriceCantBeZero);
       
    }
}
class PriceCantBeZeroInputError extends Error {

}

Possible implementations

hook into revert FAILED_REQUIRE_SIGNAL and try to map out to an enum where applicable

Also review how a developer view logs.

Blocked by: #828 #829

camsjams avatar Mar 13 '23 06:03 camsjams

is this being currently worked on? I have a solution (more a hack really) to allow for said custom errors. Can submit as a PR if an active solution isn't being built atm.

The custom error already exists within the logs field which comes from (getDecodedLogs(callResult.receipts), and is the ONLY item in logs[] if there are not other "logged" values within the Sway contract (haven't tried this out yet). The decoded logs are already available, and require a slight modification to the code to allow for the custom revert reason as mentioned in this PR as well as https://github.com/FuelLabs/fuels-ts/issues/1836 (which has some more info on this particular issue).

theausicist avatar Mar 06 '24 12:03 theausicist

Hey @theausicist 👋🏼

I believe @Dhaiwat10 is actively looking into this one, however, I'm sure it won't hurt to raise a PR if you have a solution already.

I would say that the issue #1836 is a duplicate of this one - so potentially worth closing in favour of #823 😄

petertonysmith94 avatar Mar 06 '24 13:03 petertonysmith94

Also running into this

diyahir avatar Mar 18 '24 17:03 diyahir