2

I have an issue regarding displaying errors. For errors, the logger @polkadot/api uses will write to console even when the error is caught and handled. I am handling and displaying the messages a certain way to fit my needs but then I recieve two errors, one I created and one from the @polkadot/api . I am using HttpProvider instead of WsProvider. Is there a way to suppress @polkadot/api messages?

this.provider = ApiPromise.create({ provider: httpProvider, noInitWarn: true });

This is the provider initialization which is in the class constructor.

try {
    this.provider = await this.provider;
    const nonce = await this.provider.rpc.system.accountNextIndex(address);
    return nonce;
}
catch (e) {
    console.log(e);
}

I am creating a separate class methods in which I make requests to the node. In the perfect case scenario after receiving an error I would like to handle it on my own without being displayed in the console.

Best regards!

1 Answer 1

1

You should be able to trap the provider and api errors, and then do nothing with them...

const provider = new WsProvider('wss://rpc.polkadot.io');
// events: connected, disconnected, error
provider.on('error', (err) => {
  // console.error(err)
})

const api = await ApiPromise.create({ provider });
// events: connected, disconnected, error, decorated, ready
api.on('error', (err) =>{
  // console.error(err)
})
4
  • I am not using WebSocket as a provider, instead I use HttpProvider. Dec 7, 2022 at 15:03
  • perhaps if you share some of your code we can help you better
    – DerekC
    Dec 8, 2022 at 16:10
  • 1
    Please put any code examples in the original message. That way you can use code formatting and it's easier to see. Anyway, the HttpProvider has no api.on() event, so you can't trap it. Are you able to switch to a WsProvider?
    – DerekC
    Dec 12, 2022 at 8:39
  • I've updated the original message. Unfortunately I cannot use WsProvider, because in my use case it is unreliable. Dec 12, 2022 at 15:32

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.