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
    Commented 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
    Commented Mar 21, 2022 at 13:43
  • @forgetso No, since I'm dealing with a couple of pallets, I'm asking generally
    – mkl
    Commented Mar 21, 2022 at 15:13
  • @ShawnTabrizi Oh, my bad. That's a custom format that I use in a homebrew callback logger.
    – mkl
    Commented Mar 21, 2022 at 15:14

2 Answers 2

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()}`)
        }
    }
})
0

polkadot-js got updated to a newer version, you can follow the links provided to find the new syntax, do look into what version you're on. there's a slight difference

Newer Version Syntax

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

Newer Version Syntax on sudo

Your Answer

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

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