skills-ws
Version:
87 agent skills for AI coding assistants — marketing, growth, web3, dev, design & operations. Built for OpenClaw, Claude Code, Cursor, and Codex.
37 lines (31 loc) • 1.21 kB
Markdown
## 10. Error Handling Patterns
```tsx
import { useWriteContract, useWaitForTransactionReceipt } from 'wagmi';
import { BaseError, ContractFunctionRevertedError, UserRejectedRequestError } from 'viem';
function MintNFT() {
const { writeContract, error, data: hash, isPending } = useWriteContract();
const { isLoading, isSuccess, error: receiptError } = useWaitForTransactionReceipt({ hash });
function getErrorMessage(err: Error): string {
if (err instanceof BaseError) {
const revertError = err.walk((e) => e instanceof ContractFunctionRevertedError);
if (revertError instanceof ContractFunctionRevertedError) {
return revertError.data?.errorName || 'Contract reverted';
}
if (err.walk((e) => e instanceof UserRejectedRequestError)) {
return 'Transaction rejected by user';
}
}
return err.message;
}
return (
<div>
<button onClick={() => writeContract({ /* ... */ })} disabled={isPending || isLoading}>
{isPending ? 'Confirm in wallet...' : isLoading ? 'Mining...' : 'Mint'}
</button>
{isSuccess && <p>✅ Minted! TX: {hash}</p>}
{error && <p>❌ {getErrorMessage(error)}</p>}
</div>
);
}
```
---