From 0a4480bafcc0eb60de8a73cfdd5788292a76fd4b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 19 Sep 2024 13:03:12 -0300 Subject: [PATCH] bubble error messages and improve interpretation logic (#2658) --- wormhole-connect/src/sdklegacy/errors.ts | 1 + wormhole-connect/src/utils/errors.ts | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/wormhole-connect/src/sdklegacy/errors.ts b/wormhole-connect/src/sdklegacy/errors.ts index 30447f97d..83d34bf06 100644 --- a/wormhole-connect/src/sdklegacy/errors.ts +++ b/wormhole-connect/src/sdklegacy/errors.ts @@ -14,6 +14,7 @@ export class TokenNotRegisteredError extends Error { export class InsufficientFundsForGasError extends Error { static MESSAGE = 'Insufficient funds for gas'; + static MESSAGE_REGEX = /insufficient funds|Insufficient funds for gas/gm; constructor() { super(InsufficientFundsForGasError.MESSAGE); } diff --git a/wormhole-connect/src/utils/errors.ts b/wormhole-connect/src/utils/errors.ts index 21a698f17..53c1a4db1 100644 --- a/wormhole-connect/src/utils/errors.ts +++ b/wormhole-connect/src/utils/errors.ts @@ -12,8 +12,11 @@ import { Chain } from '@wormhole-foundation/sdk'; //import { SWAP_ERROR } from 'routes/porticoBridge/consts'; // TODO SDKV2 -// copied from sdk subpackage -export const INSUFFICIENT_ALLOWANCE = 'Insufficient token allowance'; +// attempt to capture errors using regex +export const INSUFFICIENT_ALLOWANCE_REGEX = + /[I|i]nsufficient token allowance/gm; +export const USER_REJECTED_REGEX = + /rejected the request|[R|r]ejected from user|user cancel|aborted by user/gm; export function interpretTransferError( e: any, @@ -24,17 +27,17 @@ export function interpretTransferError( let internalErrorCode: TransferErrorType = ERR_UNKNOWN; if (e.message) { - if (e.message === INSUFFICIENT_ALLOWANCE) { + if (INSUFFICIENT_ALLOWANCE_REGEX.test(e?.message)) { uiErrorMessage = 'Error with transfer, please try again'; internalErrorCode = ERR_INSUFFICIENT_ALLOWANCE; } else if (e.name === 'TransactionExpiredTimeoutError') { // Solana timeout uiErrorMessage = 'Transfer timed out, please try again'; internalErrorCode = ERR_TIMEOUT; - } else if (e?.message === InsufficientFundsForGasError.MESSAGE) { + } else if (InsufficientFundsForGasError.MESSAGE_REGEX.test(e?.message)) { uiErrorMessage = e.message; internalErrorCode = ERR_INSUFFICIENT_GAS; - } else if (e.message.includes('rejected the request')) { + } else if (USER_REJECTED_REGEX.test(e?.message)) { uiErrorMessage = 'Transfer rejected in wallet, please try again'; internalErrorCode = ERR_USER_REJECTED; /* TODO SDKV2 @@ -42,6 +45,14 @@ export function interpretTransferError( uiErrorMessage = SWAP_ERROR; internalErrorCode = ERR_SWAP_FAILED; */ + } else { + /** + * if we can not interpret the error message, we show the error message if it present to + * attempt to reduce user anxiety and if the error comes from route#validate we want to + * show the error message as well. + */ + uiErrorMessage = e.message; + internalErrorCode = e.name || ERR_UNKNOWN; } }