import { TransactionManagerResponse } from '../transaction-inputs/transaction-manager-response'; import { TransactionError, TransactionManagerQueueItem } from '../types'; import { CUSTOM_ERROR_CODES, ErrorCode } from './constants'; import { TransactionErrorHandler } from './error-handler'; // class to handle top level errors export class HandleTopLevelErrors { private errorHandler: TransactionErrorHandler; constructor(errorHandler: TransactionErrorHandler) { this.errorHandler = errorHandler; } // handle top level errors public async handleTopLevelErrors( transactionQueueItem: TransactionManagerQueueItem, transactionError: TransactionError, error: any, ): Promise { // handling top level error codes if (transactionError.errorCodeTopLevel === ErrorCode.NONCE_EXPIRED) { // retry transaction return await this.errorHandler.handleNonceExpiredTransaction( transactionQueueItem, transactionError, ); } // handle action rejected if (transactionError.errorCodeTopLevel == ErrorCode.ACTION_REJECTED) { // handle transaction error return await this.errorHandler.handleTransactionFailure( transactionQueueItem, transactionError, ); } // check if transaction is call exception if ( transactionError.errorCodeTopLevel === ErrorCode.CALL_EXCEPTION && error.reason ) { return await this.errorHandler.handleTransactionFailure( transactionQueueItem, transactionError, ); } // unpredictable gas limit is equivalent to transaction failure if ( transactionError.errorCodeTopLevel === ErrorCode.UNPREDICTABLE_GAS_LIMIT ) { // you need to check further for error s do not do anything if ( transactionError.errorMessage?.includes( 'gas required exceeds allowance', ) && transactionError.errorMessage?.includes(':-32000') ) { transactionError.gasErrorCode = CUSTOM_ERROR_CODES.GAS_LIMIT_TOO_LOW; return await this.errorHandler.handleGasTransactionError( transactionQueueItem, transactionError, ); } } if ( transactionError.errorCodeTopLevel === ErrorCode.REPLACEMENT_UNDERPRICED ) { return await this.errorHandler.handleGasTransactionError( transactionQueueItem, transactionError, ); } return undefined; } }