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.

PropertyDescription
error.messageMessage trapped when the error ocurred
error.codeMetamask, 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: nullBalance: 0 ETHformattedBalance: 0.00 ETHError: 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",
},
}

See all ERROR definitions