secret.js
secret.js copied to clipboard
return error types instead of only plain text
There have been several community developers who've asked about the possibility of secretjs returning nicely typed error messages so that we don't need to do messy string matching. For example, Secret DreamScape's global error parsing looks like this:
export function handleSecretJsError(e) {
console.log(e);
if (e.message.match(/insufficient fee/g)) {
const [, amountGot, amountRequired] =
/got: (\d+)uscrt required: (\d+)uscrt/g.exec(e.message);
return alert(`Not enough gas fees. You need ${amountRequired}uscrt, but you sent ${amountGot}uscrt.`);
}
if (e.message.match(/Invalid recipient address/g)) {
return alert(`Invalid recipient address.`);
}
if (e.message.match(/out of gas/g)) {
const [, gasWanted, gasUsed] = /gasWanted: (\d+), gasUsed: (\d+)/g.exec(e.message);
return alert(`Not enough gas. You need ${gasWanted} gas, but you used ${gasUsed} gas.`);
}
if (e.message.match(/timed out waiting for tx to be included in a block/g))
return alert("Transaction timed out. Please try again.");
if (e.message.match(/Error: Account does not exist on chain. Send some tokens there before trying to query nonces./g))
return alert("Please send some SCRT to your in-game wallet address and try again.");
if (e.message.match(/insufficient funds/g)) {
const [, amountAvailable, amountRequired] =
/(\d+)uscrt is smaller than (\d+)uscrt/g.exec(e.message);
return alert(
`You don't have enough funds to complete this transaction. ${formatMoney(
parseInt(amountAvailable)
)} SCRT available, ${formatMoney(
parseInt(amountRequired)
)} SCRT required.`
);
}
let message = e.message.substr(e.message.indexOf("encrypted:") + 11);
message = message.substr(0, message.indexOf(": execute contract failed"));
const messageText = JSON.parse(message).generic_err.msg;
alert(messageText);
}
Yep, that's a good idea