6

A DispatchError will refer to a pallet and a member of the pallet's Error enum by index:

DispatchError: {"module":{"index":123,"error":456}}

It's tiresome and error-prone to look these up manually. How can I automate the process using the polkadot-js API or the web interface?

4
  • Is there a specific pallet you're interacting with?
    – forgetso
    Mar 21, 2022 at 12:42
  • Where are you seeing the dispatch error in this format? It is def something you can automate with Polkadot JS API, but maybe the web interface has not done that yet. Ultimately, you just need to map those indices to the metadata.
    – Shawn Tabrizi
    Mar 21, 2022 at 13:43
  • @forgetso No, since I'm dealing with a couple of pallets, I'm asking generally
    – mkl
    Mar 21, 2022 at 15:13
  • @ShawnTabrizi Oh, my bad. That's a custom format that I use in a homebrew callback logger.
    – mkl
    Mar 21, 2022 at 15:14

1 Answer 1

4

For polkadot-js, you can retrieve more information about the error via the event and the findMetaError api:

events
// find/filter for failed events
.filter(({ event }) =>
  api.events.system.ExtrinsicFailed.is(event)
)
// we know that data for system.ExtrinsicFailed is
// (DispatchError, DispatchInfo)
.forEach(({ event: { data: [error] } }) => {
    if (error.isModule) {
    // for module errors, we have the section indexed, lookup
    const decoded = api.registry.findMetaError(error.asModule)
    const { docs, method, section } = decoded
    console.log(`${section}.${method}: ${docs.join(' ')}`)
    } else {
        // Other, CannotLookup, BadOrigin, no extra info
        console.log(error.toString())
    }
})

This was extracted from the polkadot-js cookbook recipe "How do I get the decoded enum for an ExtrinsicFailed event".

Please note that for SUDO you have to apply a different filtering process:

events
// We know this tx should result in `Sudid` event.
.filter(({ event }) =>
  api.events.sudo.Sudid.is(event)
)
// we know Sudid just returns a result
.forEach(({ event: { data: [result] } }) => {
    // check whether extrinsic was successful
    if (result.isErr) {
        const error = result.asErr
        if (error.isModule) {
            // for module errors, we have the section indexed, lookup
            const decoded = api.registry.findMetaError(error.asModule)
            const { docs, name, section } = decoded
            console.log(`Sudo Transaction Error: ${section}.${name}: ${docs.join(' ')}`)
        } else {
            // Other, CannotLookup, BadOrigin, no extra info
            console.log(`Sudo Transaction Error: ${error.toString()}`)
        }
    }
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.