1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

TransactionsControllerTest: catch uncaught errors (#14196)

This commit is contained in:
Ariella Vu 2022-03-30 11:13:25 -03:00 committed by GitHub
parent 950de390b3
commit aac40c75ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 41 deletions

View File

@ -361,13 +361,30 @@ export default class TransactionController extends EventEmitter {
return transactions[txId]; return transactions[txId];
} }
_checkIfTxStatusIsUnapproved(txId) { /**
* @param {number} txId
* @returns {boolean}
*/
_isUnapprovedTransaction(txId) {
return ( return (
this.txStateManager.getTransaction(txId).status === this.txStateManager.getTransaction(txId).status ===
TRANSACTION_STATUSES.UNAPPROVED TRANSACTION_STATUSES.UNAPPROVED
); );
} }
/**
* @param {number} txId
* @param {string} fnName
*/
_throwErrorIfNotUnapprovedTx(txId, fnName) {
if (!this._isUnapprovedTransaction(txId)) {
throw new Error(
`TransactionsController: Can only call ${fnName} on an unapproved transaction.
Current tx status: ${this.txStateManager.getTransaction(txId).status}`,
);
}
}
_updateTransaction(txId, proposedUpdate, note) { _updateTransaction(txId, proposedUpdate, note) {
const txMeta = this.txStateManager.getTransaction(txId); const txMeta = this.txStateManager.getTransaction(txId);
const updated = merge(txMeta, proposedUpdate); const updated = merge(txMeta, proposedUpdate);
@ -416,11 +433,7 @@ export default class TransactionController extends EventEmitter {
* @returns {TransactionMeta} the txMeta of the updated transaction * @returns {TransactionMeta} the txMeta of the updated transaction
*/ */
updateEditableParams(txId, { data, from, to, value, gas, gasPrice }) { updateEditableParams(txId, { data, from, to, value, gas, gasPrice }) {
if (!this._checkIfTxStatusIsUnapproved(txId)) { this._throwErrorIfNotUnapprovedTx(txId, 'updateEditableParams');
throw new Error(
'Cannot call updateEditableParams on a transaction that is not in an unapproved state',
);
}
const editableParams = { const editableParams = {
txParams: { txParams: {
@ -474,11 +487,7 @@ export default class TransactionController extends EventEmitter {
userFeeLevel, userFeeLevel,
}, },
) { ) {
if (!this._checkIfTxStatusIsUnapproved(txId)) { this._throwErrorIfNotUnapprovedTx(txId, 'updateTransactionGasFees');
throw new Error(
'Cannot call updateTransactionGasFees on a transaction that is not in an unapproved state',
);
}
let txGasFees = { let txGasFees = {
txParams: { txParams: {
@ -517,11 +526,10 @@ export default class TransactionController extends EventEmitter {
txId, txId,
{ estimatedBaseFee, decEstimatedBaseFee }, { estimatedBaseFee, decEstimatedBaseFee },
) { ) {
if (!this._checkIfTxStatusIsUnapproved(txId)) { this._throwErrorIfNotUnapprovedTx(
throw new Error( txId,
'Cannot call updateTransactionEstimatedBaseFee on a transaction that is not in an unapproved state', 'updateTransactionEstimatedBaseFee',
); );
}
let txEstimateBaseFees = { estimatedBaseFee, decEstimatedBaseFee }; let txEstimateBaseFees = { estimatedBaseFee, decEstimatedBaseFee };
// only update what is defined // only update what is defined
@ -543,11 +551,7 @@ export default class TransactionController extends EventEmitter {
* @returns {TransactionMeta} the txMeta of the updated transaction * @returns {TransactionMeta} the txMeta of the updated transaction
*/ */
updateSwapApprovalTransaction(txId, { type, sourceTokenSymbol }) { updateSwapApprovalTransaction(txId, { type, sourceTokenSymbol }) {
if (!this._checkIfTxStatusIsUnapproved(txId)) { this._throwErrorIfNotUnapprovedTx(txId, 'updateSwapApprovalTransaction');
throw new Error(
'Cannot call updateSwapApprovalTransaction on a transaction that is not in an unapproved state',
);
}
let swapApprovalTransaction = { type, sourceTokenSymbol }; let swapApprovalTransaction = { type, sourceTokenSymbol };
// only update what is defined // only update what is defined
@ -589,11 +593,7 @@ export default class TransactionController extends EventEmitter {
approvalTxId, approvalTxId,
}, },
) { ) {
if (!this._checkIfTxStatusIsUnapproved(txId)) { this._throwErrorIfNotUnapprovedTx(txId, 'updateSwapTransaction');
throw new Error(
'Cannot call updateSwapTransaction on a transaction that is not in an unapproved state',
);
}
let swapTransaction = { let swapTransaction = {
sourceTokenSymbol, sourceTokenSymbol,
@ -625,11 +625,7 @@ export default class TransactionController extends EventEmitter {
* @returns {TransactionMeta} the txMeta of the updated transaction * @returns {TransactionMeta} the txMeta of the updated transaction
*/ */
updateTransactionUserSettings(txId, { userEditedGasLimit, userFeeLevel }) { updateTransactionUserSettings(txId, { userEditedGasLimit, userFeeLevel }) {
if (!this._checkIfTxStatusIsUnapproved(txId)) { this._throwErrorIfNotUnapprovedTx(txId, 'updateTransactionUserSettings');
throw new Error(
'Cannot call updateTransactionUserSettings on a transaction that is not in an unapproved state',
);
}
let userSettings = { userEditedGasLimit, userFeeLevel }; let userSettings = { userEditedGasLimit, userFeeLevel };
// only update what is defined // only update what is defined

View File

@ -2186,10 +2186,10 @@ describe('Transaction Controller', function () {
assert.equal(result.userFeeLevel, 'high'); assert.equal(result.userFeeLevel, 'high');
}); });
it('throws error if status is not unapproved', function () { it('should not update and should throw error if status is not type "unapproved"', function () {
txStateManager.addTransaction({ txStateManager.addTransaction({
id: '4', id: '4',
status: TRANSACTION_STATUSES.APPROVED, status: TRANSACTION_STATUSES.DROPPED,
metamaskNetworkId: currentNetworkId, metamaskNetworkId: currentNetworkId,
txParams: { txParams: {
maxPriorityFeePerGas: '0x007', maxPriorityFeePerGas: '0x007',
@ -2200,14 +2200,18 @@ describe('Transaction Controller', function () {
estimateUsed: '0x009', estimateUsed: '0x009',
}); });
try { assert.throws(
txController.updateTransactionGasFees('4', { maxFeePerGas: '0x0088' }); () =>
} catch (e) { txController.updateTransactionGasFees('4', {
assert.equal( maxFeePerGas: '0x0088',
e.message, }),
'Cannot call updateTransactionGasFees on a transaction that is not in an unapproved state', Error,
); `TransactionsController: Can only call updateTransactionGasFees on an unapproved transaction.
} Current tx status: ${TRANSACTION_STATUSES.DROPPED}`,
);
const transaction = txStateManager.getTransaction('4');
assert.equal(transaction.txParams.maxFeePerGas, '0x008');
}); });
it('does not update unknown parameters in update method', function () { it('does not update unknown parameters in update method', function () {