Any error that occurs using any provided hook, function or utility can be consumed at the .error
prop from useMetamak
hook.
error
has two properties and is null
when no error presented.
Property | Description |
---|---|
error.message | Message trapped when the error ocurred |
error.code | Metamask, forta or RPC related code error |
Example
Try to connect and Cancel/Deny the request. This will update the error state for the hook
⚠️
Note that .error
is null
when there's no error state.
import { useMetamask } from "@forta/usemetamask"
function Connect() { const { error } = useMetamask() return ( <> <code>Error: {error ? error.message : "null"}</code> <button onClick={connect}>CONNECT</button> </> )}
Address: null
Balance: 0 ETH
formattedBalance: 0.00 ETH
Error: null
Reset/Clear Error state
The hook will clear any error state when account
or accounts
props change.
In some cases you want to clear this state like when re-starting a flow or so.
To clear .error
just call the resetError
method from useMetamask
hook.
import { useMetamask } from "@forta/usemetamask"
function ConnectReset() { const { account, error, resetError } = useMetamask() return ( <> <code>Address: {account || "null"}</code> <code>Error: {error ? error.message : "null"}</code> <button onClick={resetError}>Reset error</button> </> )}
Forta error definitions
export const ERRORS = { METAMASK_NOT_INSTALLED: { code: -600, message: "Metamask not installed", }, USER_NOT_CONNECTED: { code: -601, message: "User not connected", },}