Skip to content

Commit

Permalink
fix: Currently keystone lib will throw KeystoneError#Tx_canceled wh…
Browse files Browse the repository at this point in the history
…ich will crash the mobile app (#13313)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**
This PR will fix #13259 issue.
When user reject signature in QR screen, the
`@keystonehq/metamask-airgapped-keyring` will throw
`KeystoneError#Tx_canceled` error. however, for some reason, Hermes
engine threat that error as object and go to Crash flow.
I have fixed it by adding above uncatch error into our global
ErrorHandler.ts to specially thread it.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Related issues**

Fixes:

## **Manual testing steps**
```
  Scenario: Swap with QR account and Reject QR modal
  
  GIVEN I have QR account selected
  WHEN I swap
  AND Reject QR modal
  THEN modal is closed (FAILED: app closes and crash alert is displayed)
  AND app is still functioning (FAILED)
```
## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

https://github.com/user-attachments/assets/6313a2fa-e385-4a58-b7ac-32d5d9d422cf

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
dawnseeker8 authored Feb 3, 2025
1 parent ef3b5d1 commit d01cf65
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
36 changes: 31 additions & 5 deletions app/core/ErrorHandler/ErrorHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getReactNativeDefaultHandler, handleCustomError, setReactNativeDefaultHandler } from './ErrorHandler';
import {
getReactNativeDefaultHandler,
handleCustomError,
setReactNativeDefaultHandler,
} from './ErrorHandler';
import { ErrorHandlerCallback } from 'react-native';

describe('ErrorHandler', () => {
Expand All @@ -10,10 +14,16 @@ describe('ErrorHandler', () => {
});

it('handles Ledger error without crashing the app', () => {
const mockError = { name: 'EthAppPleaseEnableContractData', message: 'Enable contract data' };
const mockError = {
name: 'EthAppPleaseEnableContractData',
message: 'Enable contract data',
};
console.error = jest.fn();
handleCustomError(mockError, true);
expect(console.error).toHaveBeenCalledWith('Ledger error: ', 'Enable contract data');
expect(console.error).toHaveBeenCalledWith(
'Ledger error: ',
'Enable contract data',
);
});

it('passes non-Ledger error to the default handler', () => {
Expand All @@ -24,9 +34,25 @@ describe('ErrorHandler', () => {
});

it('handles TransportStatusError without crashing the app', () => {
const mockError = { name: 'TransportStatusError', message: 'Transport error' };
const mockError = {
name: 'TransportStatusError',
message: 'Transport error',
};
console.error = jest.fn();
handleCustomError(mockError, true);
expect(console.error).toHaveBeenCalledWith('Ledger error: ', 'Transport error');
expect(console.error).toHaveBeenCalledWith(
'Ledger error: ',
'Transport error',
);
});

it('handles KeystoneError#Tx_canceled without crashing the app', () => {
const mockError = { name: 'Error', message: 'KeystoneError#Tx_canceled' };
console.error = jest.fn();
handleCustomError(mockError, true);
expect(console.error).toHaveBeenCalledWith(
'Keystone error: ',
'KeystoneError#Tx_canceled',
);
});
});
26 changes: 17 additions & 9 deletions app/core/ErrorHandler/ErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ export const setReactNativeDefaultHandler = (handler: ErrorHandlerCallback) => {
};

export const handleCustomError = (error: Error, isFatal: boolean) => {
// Check whether the error is from the Ledger native bluetooth errors.
if(error.name === 'EthAppPleaseEnableContractData' || error.name === 'TransportStatusError') {
// dont pass the error to react native error handler to prevent app crash
console.error('Ledger error: ', error.message);
// Handle the error
} else {
// Pass the error to react native error handler
reactNativeDefaultHandler(error, isFatal);
}
// Check whether the error is from the Ledger native bluetooth errors.
if (
error.name === 'EthAppPleaseEnableContractData' ||
error.name === 'TransportStatusError'
) {
// dont pass the error to react native error handler to prevent app crash
console.error('Ledger error: ', error.message);
// check error message contain "KeystoneError#Tx_canceled"
} else if (
error.name === 'Error' &&
error.message?.includes('KeystoneError#Tx_canceled')
) {
console.error('Keystone error: ', error.message);
} else {
// Pass the error to react native error handler
reactNativeDefaultHandler(error, isFatal);
}
};

export const getReactNativeDefaultHandler = () => reactNativeDefaultHandler;

0 comments on commit d01cf65

Please sign in to comment.