Skip to content

Commit 0a4480b

Browse files
bubble error messages and improve interpretation logic (#2658)
1 parent 3acc965 commit 0a4480b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

wormhole-connect/src/sdklegacy/errors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class TokenNotRegisteredError extends Error {
1414

1515
export class InsufficientFundsForGasError extends Error {
1616
static MESSAGE = 'Insufficient funds for gas';
17+
static MESSAGE_REGEX = /insufficient funds|Insufficient funds for gas/gm;
1718
constructor() {
1819
super(InsufficientFundsForGasError.MESSAGE);
1920
}

wormhole-connect/src/utils/errors.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import { Chain } from '@wormhole-foundation/sdk';
1212
//import { SWAP_ERROR } from 'routes/porticoBridge/consts';
1313

1414
// TODO SDKV2
15-
// copied from sdk subpackage
16-
export const INSUFFICIENT_ALLOWANCE = 'Insufficient token allowance';
15+
// attempt to capture errors using regex
16+
export const INSUFFICIENT_ALLOWANCE_REGEX =
17+
/[I|i]nsufficient token allowance/gm;
18+
export const USER_REJECTED_REGEX =
19+
/rejected the request|[R|r]ejected from user|user cancel|aborted by user/gm;
1720

1821
export function interpretTransferError(
1922
e: any,
@@ -24,24 +27,32 @@ export function interpretTransferError(
2427
let internalErrorCode: TransferErrorType = ERR_UNKNOWN;
2528

2629
if (e.message) {
27-
if (e.message === INSUFFICIENT_ALLOWANCE) {
30+
if (INSUFFICIENT_ALLOWANCE_REGEX.test(e?.message)) {
2831
uiErrorMessage = 'Error with transfer, please try again';
2932
internalErrorCode = ERR_INSUFFICIENT_ALLOWANCE;
3033
} else if (e.name === 'TransactionExpiredTimeoutError') {
3134
// Solana timeout
3235
uiErrorMessage = 'Transfer timed out, please try again';
3336
internalErrorCode = ERR_TIMEOUT;
34-
} else if (e?.message === InsufficientFundsForGasError.MESSAGE) {
37+
} else if (InsufficientFundsForGasError.MESSAGE_REGEX.test(e?.message)) {
3538
uiErrorMessage = e.message;
3639
internalErrorCode = ERR_INSUFFICIENT_GAS;
37-
} else if (e.message.includes('rejected the request')) {
40+
} else if (USER_REJECTED_REGEX.test(e?.message)) {
3841
uiErrorMessage = 'Transfer rejected in wallet, please try again';
3942
internalErrorCode = ERR_USER_REJECTED;
4043
/* TODO SDKV2
4144
} else if (e.message === SWAP_ERROR) {
4245
uiErrorMessage = SWAP_ERROR;
4346
internalErrorCode = ERR_SWAP_FAILED;
4447
*/
48+
} else {
49+
/**
50+
* if we can not interpret the error message, we show the error message if it present to
51+
* attempt to reduce user anxiety and if the error comes from route#validate we want to
52+
* show the error message as well.
53+
*/
54+
uiErrorMessage = e.message;
55+
internalErrorCode = e.name || ERR_UNKNOWN;
4556
}
4657
}
4758

0 commit comments

Comments
 (0)