@@ -12,8 +12,11 @@ import { Chain } from '@wormhole-foundation/sdk';
12
12
//import { SWAP_ERROR } from 'routes/porticoBridge/consts';
13
13
14
14
// 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 ] n s u f f i c i e n t t o k e n a l l o w a n c e / gm;
18
+ export const USER_REJECTED_REGEX =
19
+ / r e j e c t e d t h e r e q u e s t | [ R | r ] e j e c t e d f r o m u s e r | u s e r c a n c e l | a b o r t e d b y u s e r / gm;
17
20
18
21
export function interpretTransferError (
19
22
e : any ,
@@ -24,24 +27,32 @@ export function interpretTransferError(
24
27
let internalErrorCode : TransferErrorType = ERR_UNKNOWN ;
25
28
26
29
if ( e . message ) {
27
- if ( e . message === INSUFFICIENT_ALLOWANCE ) {
30
+ if ( INSUFFICIENT_ALLOWANCE_REGEX . test ( e ? .message ) ) {
28
31
uiErrorMessage = 'Error with transfer, please try again' ;
29
32
internalErrorCode = ERR_INSUFFICIENT_ALLOWANCE ;
30
33
} else if ( e . name === 'TransactionExpiredTimeoutError' ) {
31
34
// Solana timeout
32
35
uiErrorMessage = 'Transfer timed out, please try again' ;
33
36
internalErrorCode = ERR_TIMEOUT ;
34
- } else if ( e ?. message === InsufficientFundsForGasError . MESSAGE ) {
37
+ } else if ( InsufficientFundsForGasError . MESSAGE_REGEX . test ( e ?. message ) ) {
35
38
uiErrorMessage = e . message ;
36
39
internalErrorCode = ERR_INSUFFICIENT_GAS ;
37
- } else if ( e . message . includes ( 'rejected the request' ) ) {
40
+ } else if ( USER_REJECTED_REGEX . test ( e ? .message ) ) {
38
41
uiErrorMessage = 'Transfer rejected in wallet, please try again' ;
39
42
internalErrorCode = ERR_USER_REJECTED ;
40
43
/* TODO SDKV2
41
44
} else if (e.message === SWAP_ERROR) {
42
45
uiErrorMessage = SWAP_ERROR;
43
46
internalErrorCode = ERR_SWAP_FAILED;
44
47
*/
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 ;
45
56
}
46
57
}
47
58
0 commit comments