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

useScaffoldContractWrite does not throw errors #753

Closed
1 task done
corwinthill opened this issue Mar 6, 2024 · 1 comment · Fixed by #758
Closed
1 task done

useScaffoldContractWrite does not throw errors #753

corwinthill opened this issue Mar 6, 2024 · 1 comment · Fixed by #758

Comments

@corwinthill
Copy link
Contributor

Is there an existing issue for this?

Which method was used to setup Scaffold-ETH 2 ?

git clone, npx create-eth@latest

Current Behavior

useScaffoldContractWrite does not throw errors, but instead eats up the errors and displays it using the notification util. When calling a contract function using useScaffoldContractWrite within a try/catch block, regardless of whether the function call succeeds or fails, no exception is caught and the try block is fully executed. The current behavior thus allows for an error notification and a success notification to simultaneously appear.

Expected Behavior

From my understanding, the intended functionality should be that useScaffoldContractWrite should throw an error so that functions utilizing it can be used within try/catch blocks. useScaffoldContractWrite relies on useTransactor, so an error caught by useTransactor should be thrown to useScaffoldContractWrite, and the error should then be thrown forward again to the contract function that is utilizing useScaffoldContractWrite. There should also only be one instance of the error notification being displayed.

Steps To Reproduce

The issue comes from a setup similar to the code below.

const { writeAsync: exampleFunction } = useScaffoldContractWrite({
  contractName: "exampleContract",
  functionName: "exampleFunction",
});

const handleExampleFunction = async () => {
  const loadingNotification = notification.loading("Loading ...");
  try {
    await exampleFunction();
    notification.remove(loadingNotification);
    notification.success("Success!");
  } catch (error) {
    notification.remove(loadingNotification);
    console.error(error);
  }
};

You'll want something on the frontend to trigger the function call (like a button). Then make sure that the function call will revert so that an error is thrown. A simple one to setup is not having any funds for gas. Once the function is called, then both the Success notification, and an error notification will be displayed.

Anything else?

I'm not sure if its the best solution, but I have resolved this issue with the help of Shiv Bhonde. A few lines of code need to be changed on useScaffoldContractWrite.ts as well as useTransactor.tsx.
First, I added the following code after the line referenced in the link to useScaffoldContractWrite.ts

// Insert after line 87
throw new Error(e);

Then, I modified useTransactor.tsx as follows

// Comment out line 98, otherwise two error notifications will display
// One from useTransactor, and then another in useScaffoldContractWrite
// Insert next line after line 98
throw new Error(message);

I believe this properly handles where the error ends up. Originally it was getting lost in the useTransactor functionality. After these changes, if an error occurs, it should be thrown forward to the appropriate location.

@technophile-04
Copy link
Collaborator

Hey thanks @corwinthill !! I think the solution looks good ! Do you want to create a PR with it ?

Please let us know if you need any help 🙌

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

Successfully merging a pull request may close this issue.

2 participants