From 5c969429221c985405512184233b2d910c03f4d6 Mon Sep 17 00:00:00 2001 From: Moody Salem Date: Thu, 13 May 2021 09:02:48 -0500 Subject: [PATCH] fix: show better errors --- src/hooks/useSwapCallback.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSwapCallback.ts b/src/hooks/useSwapCallback.ts index e601471eae..da4c5b8f17 100644 --- a/src/hooks/useSwapCallback.ts +++ b/src/hooks/useSwapCallback.ts @@ -133,8 +133,21 @@ function useSwapCallArguments( }, [account, allowedSlippage, chainId, deadline, library, recipient, routerContract, signatureData, trade]) } -export function swapErrorToUserReadableMessage(error: { reason: string }): string { - switch (error.reason) { +/** + * This is hacking out the revert reason from the ethers provider thrown error however it can. + * This object seems to be undocumented by ethers. + * @param error an error from the ethers provider + */ +export function swapErrorToUserReadableMessage(error: any): string { + let reason: string | undefined + while (Boolean(error)) { + reason = error.reason ?? error.message ?? reason + error = error.error ?? error.data.originalError + } + + if (reason?.indexOf('execution reverted: ') === 0) reason = reason.substr('execution reverted: '.length) + + switch (reason) { case 'UniswapV2Router: EXPIRED': return 'The transaction could not be sent because the deadline has passed. Please check that your transaction deadline is not too low.' case 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT':