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