0

I have a call to an extrinsic (in a Typescript/RxJS client) that is failing but I want to be able to catch the error that the extrinsic is throwing and not just that it failed.

const chainEvents: EventMap = await myExtrinsic(api, keys, somedata);
const failedEvent: Event = chainEvents["system.ExtrinsicFailed"];

So how do I find the specific error that the chain is throwing (i.e. from Rust)? I want the DispatchError.

Furthermore, the output isn't very helpful:

failedEvent.index=undefined
failedEvent.method=ExtrinsicFailed
failedEvent.section=system
failedEvent.data=undefined

Error from call to api

Error in Polkadot.js UI

3
  • 1
    Maybe this post will help you: substrate.stackexchange.com/questions/947/…. And doesn't it fail at ensure_signed/ensure_root (BadOrigin)? Dec 13, 2022 at 7:59
  • That code doesn't work but may reveal some clues. I was able to get the index of the error in the enum BUT not a human readable description. ie. the actual enum name and the pallet where the enum is defined. const failedEvent = chainEvents["system.ExtrinsicFailed"]; console.dir(failedEvent.toJSON()); displays: [ { module: { index: 61, error: '0x03000000' } }, { weight: 288499000, class: 'Normal', paysFee: 'Yes' } ] where 0x03000000 means the enum at index 3 (4th when counting from 0) Dec 13, 2022 at 22:09
  • There should be a more direct way of getting this or at least a way to get a human readable error enum name and pallet name. Dec 13, 2022 at 22:11

1 Answer 1

2

This piece of code should work if you apply it to your custom pallet/extrinsic

api.tx.balances
  .transfer(recipient, 123)
  .signAndSend(sender, ({ status, events, dispatchError }) => {
    // status would still be set, but in the case of error we can shortcut
    // to just check it (so an error would indicate InBlock or Finalized)
    if (dispatchError) {
      if (dispatchError.isModule) {
        // for module errors, we have the section indexed, lookup
        const decoded = api.registry.findMetaError(dispatchError.asModule);
        const { docs, name, section } = decoded;

        console.log(`${section}.${name}: ${docs.join(' ')}`);
      } else {
        // Other, CannotLookup, BadOrigin, no extra info
        console.log(dispatchError.toString());
      }
    }
  });

Reference from https://polkadot.js.org/docs/api/cookbook/tx/#how-do-i-get-the-decoded-enum-for-an-extrinsicfailed-event

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.