Skip to content

Commit

Permalink
bubble error messages and improve interpretation logic (#2658)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianscatularo authored Sep 19, 2024
1 parent 3acc965 commit 0a4480b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions wormhole-connect/src/sdklegacy/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 16 additions & 5 deletions wormhole-connect/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,24 +27,32 @@ 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
} else if (e.message === SWAP_ERROR) {
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;
}
}

Expand Down

0 comments on commit 0a4480b

Please sign in to comment.