WDK logoWDK documentation

Handle Errors

Catch Morpho lending failures and release wallet secrets safely.

This guide explains how to handle operation errors, handle requirement errors, and follow best practices for disposing wallet state.

Operation errors

You can catch failures from supply(), withdraw(), supplyCollateral(), borrow(), repay(), and withdrawCollateral() with try/catch:

Handle a failed supply
try {
  await morpho.supply({
    token: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    amount: 0n
  })
} catch (e) {
  console.error('Morpho lending failed:', e.message)

  if (e.message.includes('amount')) {
    console.log('Amount must be greater than zero')
  }
}

Common failure causes include:

  • wallet account does not have a provider configured
  • write method is called with a read-only account
  • token does not match the configured vault asset, market loan token, or market collateral token
  • explicit target is used without the required chainId
  • connected chain does not match the configured Morpho target
  • amount is zero, invalid, or larger than the account balance
  • onBehalfOf or to does not match the connected wallet address when required

Use this checklist to map the most common failures to fixes:

SymptomLikely causeFix
Constructor fails before any operationWallet account has no providerCreate the EVM account with a provider or use a read-only account with an RPC provider
Write method fails on a read-only accountRead-only account can quote and read, but cannot send transactionsUse WalletAccountEvm or WalletAccountEvmErc4337 for mutating methods
Explicit target fails during setupchainId is missing, invalid, or does not match the wallet chainPass the expected chainId with earnVaultAddress, borrowMarketId, or borrowMarketParams
Token mismatch errorThe supplied token is not the configured vault asset, market loan token, or collateral tokenUse the token from the configured vault or market target
Zero amount erroramount and nativeAmount are both absent or zeroPass a positive ERC-20 amount, a positive nativeAmount, or amount: "max" for repay
Requirement lookup returns approval or authorizationAllowance, permit, Permit2, or Morpho authorization is missingSend returned transaction requirements or sign the returned signature requirement before the final action
Final action fails after requirementsAllowance, balance, signature, authorization, quote, or market state changed after the requirement lookupRe-run the matching get*Requirements() method and rebuild the final action
Quote succeeds but write failsOn-chain state changed, the account lacks balance, or requirements were not satisfiedRe-check requirements, balances, token addresses, and chain before sending

Requirement errors

Requirement helpers can fail if the configured target, token, account, or provider cannot produce a valid Morpho SDK action.

Handle requirement errors
try {
  const requirements = await morpho.getBorrowRequirements({
    token: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    amount: 1000000n
  })

  console.log('Borrow requirements:', requirements)
} catch (e) {
  console.error('Requirement lookup failed:', e.message)
}

If a final action fails after requirements were returned, re-check the requirements. Allowances, signatures, authorizations, or on-chain market state can change between the requirement lookup and the final transaction.

Quote errors

You can isolate quote failures from write failures when you only need an estimate:

Handle quote errors
try {
  const quote = await morpho.quoteBorrow({
    token: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    amount: 1000000n
  })

  console.log('Borrow fee:', quote.fee)
} catch (e) {
  console.error('Quote failed:', e.message)
}

See Rules & Notes for address, token, amount, and target validation expectations.

Best Practices

Dispose wallet secrets after a lending session by calling dispose() on WalletAccountEvm, or the matching dispose method on your smart account type.

Dispose after a Morpho lending session
try {
  await morpho.supply({
    token: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    amount: 1000000n
  })
} finally {
  account.dispose()
}

For ERC-4337 accounts, use dispose() on the smart-account type. Clear references to MorphoProtocolEvm when the session ends.

Next Steps

On this page