Skip to content
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

return error types instead of only plain text #80

Open
zorostang opened this issue Mar 14, 2022 · 1 comment
Open

return error types instead of only plain text #80

zorostang opened this issue Mar 14, 2022 · 1 comment

Comments

@zorostang
Copy link

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);
}
@Cashmaney
Copy link
Member

Yep, that's a good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants