-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unhandled Rejection when insufficient balance #6040
Comments
Hey, do you mind posting a short example that would demonstrate the above? |
Sure, here is the code I use in my app (from the repository examples) const api = await initialize(AVAIL_URL);
const account = getKeyringFromSeed(AVAIL_MNEMONIC);
const data = AbiCoder.defaultAbiCoder().encode(['string[]'], [DATA_TO_PUSH]);
const txResult: any = await new Promise((res) => {
api.tx.dataAvailability.submitData(data).signAndSend(
account,
{
// @ts-expect-error Avail uses snake_case for app id
app_id: AVAIL_APP_ID,
},
(result: any) => {
if (result.isFinalized || result.isError) {
res(result);
}
},
);
}); |
@0xmemorygrinder Can you expand on your issue 🙏🏻. It seems like just wrapping your transaction call in a try catch should be enough to successfully handle an error triggered by a failed transaction. Here's a simple example: import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
import { AccId32 } from "./dev-accounts.json";
const CHAIN_ENDPOINT = "ws://127.0.0.1:9988";
const main = async () => {
const api = await ApiPromise.create({
provider: new WsProvider(CHAIN_ENDPOINT),
});
await api.isReady;
const keyring = new Keyring({ type: "sr25519" });
const unfundedSender = keyring.addFromUri("//ZeroBalanceSeed");
try {
// This will fail since the sender has no funds
const res = await api.tx.balances
.transferKeepAlive(AccId32.BOB, 1000000000)
.signAndSend(unfundedSender);
console.log(res.toHuman());
} catch (error) {
console.log("Handle the error correctly:", error);
}
console.log("--- Continue no problem after error ---");
const alice = keyring.addFromUri("//Alice");
try {
//Should succed
const res = await api.tx.balances
.transferKeepAlive(AccId32.BOB, 1000000000)
.signAndSend(alice);
console.log("Succesful:", res.toHuman());
} catch (error) {
console.log(error);
}
};
main(); Output logs
As you can see it was able to handle the error and the continue with the execution. Is there something I'm missing from your example? Please feel free to provide more details |
When submitting a tx with an account that does not have sufficient gas for fee, an error is thrown but it is never catched by the calling functions so it ends up in an unhandled rejection error.
Since nodejs 15, the default behavior when an error like this arise is to exit the process with code 1. In other terms if I submit a transaction with not enough balance, my app crashes without asking anything.
This behavior can be prevented by adding a global error handler and while being hacky, the signAndSend method never triggers the passed callback.
The expected behavior is to catch correctly errors and have a correct callback trigger with the error inside.
The app crashing on a so trivial error should be a pretty straightforward motivation.
Version:
Environment:
Language:
The text was updated successfully, but these errors were encountered: