Skip to content

Commit

Permalink
Update error handling in sendTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrotkk committed Mar 24, 2024
1 parent c5f6893 commit d26577a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const DEFAULT_MIN_SFUEL_WEI = 21000000000000n
export const DEFAULT_ERC20_DECIMALS = '18'
export const DEFAULT_ERROR_MSG = 'Ooops... Something went wrong...'
export const TRANSFER_ERROR_MSG = 'Error during the transfer'
export const TRANSACTION_ERROR_MSG = 'Transaction sending failed'

export const DEFAULT_MP_MARGIN = '20pt'
export const DEFAULT_MP_Z_INDEX = 99000
Expand Down
20 changes: 17 additions & 3 deletions src/core/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

import { type TransactionResponse } from 'ethers';
import { TRANSFER_ERROR_MSG } from './constants'
import { TRANSACTION_ERROR_MSG } from './constants'
import { TxResponse } from './interfaces'


Expand All @@ -38,8 +38,10 @@ export async function sendTransaction(func: any, args: any[]): Promise<TxRespons
if (err.code && err.code === 'ACTION_REJECTED') {
name = 'Transaction signing was rejected'
} else {
name = TRANSFER_ERROR_MSG
name = TRANSACTION_ERROR_MSG
}
const revertMsg = parseErrorMessage(err.message)
if (revertMsg) name = revertMsg
if (err.info && err.info.error && err.info.error.data && err.info.error.data.message) {
name = err.info.error.data.message
}
Expand All @@ -48,4 +50,16 @@ export async function sendTransaction(func: any, args: any[]): Promise<TxRespons
}
return { status: false, err: { name, msg }, response: undefined }
}
}
}


function parseErrorMessage(input: string | undefined): string | null {
if (!input) return null
const startDelimiter = 'execution reverted: "';
const endDelimiter = '"';
const startIndex = input.indexOf(startDelimiter);
if (startIndex === -1) return null
const endIndex = input.indexOf(endDelimiter, startIndex + startDelimiter.length);
if (endIndex === -1) return null
return input.substring(startIndex + startDelimiter.length, endIndex);
}

0 comments on commit d26577a

Please sign in to comment.