Skip to content

Commit

Permalink
fix: add unhandle KeystoneError#Tx_canceled exception to Custom Err…
Browse files Browse the repository at this point in the history
…or handler to prevent it crash the hermes engine.
  • Loading branch information
dawnseeker8 committed Feb 3, 2025
1 parent 10a5191 commit 3f111a8
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 3f111a8

Please sign in to comment.