FIX: Unhandled promise rejection on GC

This commit is contained in:
Marcos Rodriguez Velez 2024-10-23 22:09:22 -04:00
parent cebdea6d25
commit e1e34b17a0
4 changed files with 78 additions and 28 deletions

View File

@ -190,24 +190,54 @@ function Notifications(props) {
* @returns {Promise<object>} Response object from API rest call
*/
Notifications.majorTomToGroundControl = async function (addresses, hashes, txids) {
if (!Array.isArray(addresses) || !Array.isArray(hashes) || !Array.isArray(txids))
throw new Error('no addresses or hashes or txids provided');
const pushToken = await Notifications.getPushToken();
if (!pushToken || !pushToken.token || !pushToken.os) return;
try {
if (!Array.isArray(addresses) || !Array.isArray(hashes) || !Array.isArray(txids)) {
throw new Error('No addresses, hashes, or txids provided');
}
const response = await fetch(`${baseURI}/majorTomToGroundControl`, {
method: 'POST',
headers: _getHeaders(),
body: JSON.stringify({
const pushToken = await Notifications.getPushToken();
if (!pushToken || !pushToken.token || !pushToken.os) {
return;
}
const requestBody = JSON.stringify({
addresses,
hashes,
txids,
token: pushToken.token,
os: pushToken.os,
}),
});
});
return response.json();
let response;
try {
response = await fetch(`${baseURI}/majorTomToGroundControl`, {
method: 'POST',
headers: _getHeaders(),
body: requestBody,
});
} catch (networkError) {
console.error('Network request failed:', networkError);
throw networkError;
}
if (!response.ok) {
return;
}
const responseText = await response.text();
if (responseText) {
try {
return JSON.parse(responseText);
} catch (jsonError) {
console.error('Error parsing response JSON:', jsonError);
throw jsonError;
}
} else {
return {}; // Return an empty object if there is no response body
}
} catch (error) {
console.error('Error in majorTomToGroundControl:', error);
}
};
/**

View File

@ -5,6 +5,9 @@ import { scanQrHelper } from '../helpers/scan-qr';
import loc from '../loc';
import presentAlert from './Alert';
import ToolTipMenu from './TooltipMenu';
import { useTheme } from './themes';
import { showFilePickerAndReadFile, showImagePickerAndReadImage } from '../blue_modules/fs';
import Clipboard from '@react-native-clipboard/clipboard';
interface AddressInputProps {
isLoading?: boolean;

View File

@ -107,15 +107,20 @@ const ReceiveDetails = () => {
let newAddress;
if (address) {
setAddressBIP21Encoded(address);
await Notifications.tryToObtainPermissions(receiveAddressButton);
Notifications.majorTomToGroundControl([address], [], []);
try {
await Notifications.tryToObtainPermissions(receiveAddressButton);
Notifications.majorTomToGroundControl([address], [], []);
} catch (error) {
console.error('Error obtaining notifications permissions:', error);
}
} else {
if (wallet.chain === Chain.ONCHAIN) {
try {
if (!isElectrumDisabled) newAddress = await Promise.race([wallet.getAddressAsync(), sleep(1000)]);
} catch (_) {}
} catch (error) {
console.warn('Error fetching wallet address (ONCHAIN):', error);
}
if (newAddress === undefined) {
// either sleep expired or getAddressAsync threw an exception
console.warn('either sleep expired or getAddressAsync threw an exception');
newAddress = wallet._getExternalAddressByIndex(wallet.getNextFreeAddressIndex());
} else {
@ -125,9 +130,10 @@ const ReceiveDetails = () => {
try {
await Promise.race([wallet.getAddressAsync(), sleep(1000)]);
newAddress = wallet.getAddress();
} catch (_) {}
} catch (error) {
console.warn('Error fetching wallet address (OFFCHAIN):', error);
}
if (newAddress === undefined) {
// either sleep expired or getAddressAsync threw an exception
console.warn('either sleep expired or getAddressAsync threw an exception');
newAddress = wallet.getAddress();
} else {
@ -135,8 +141,12 @@ const ReceiveDetails = () => {
}
}
setAddressBIP21Encoded(newAddress);
await Notifications.tryToObtainPermissions(receiveAddressButton);
Notifications.majorTomToGroundControl([newAddress], [], []);
try {
await Notifications.tryToObtainPermissions(receiveAddressButton);
Notifications.majorTomToGroundControl([newAddress], [], []);
} catch (error) {
console.error('Error obtaining notifications permissions:', error);
}
}
}, [wallet, saveToDisk, address, setAddressBIP21Encoded, isElectrumDisabled, sleep]);
@ -167,7 +177,6 @@ const ReceiveDetails = () => {
const HeaderRight = useMemo(
() => <HeaderMenuButton actions={toolTipActions} onPressMenuItem={onPressMenuItem} />,
[onPressMenuItem, toolTipActions],
);
@ -271,7 +280,7 @@ const ReceiveDetails = () => {
}
}
} catch (error) {
console.debug(error);
console.debug('Error checking balance:', error);
}
}, intervalMs);
@ -363,10 +372,14 @@ const ReceiveDetails = () => {
useFocusEffect(
useCallback(() => {
const task = InteractionManager.runAfterInteractions(async () => {
if (wallet) {
obtainWalletAddress();
} else if (!wallet && address) {
setAddressBIP21Encoded(address);
try {
if (wallet) {
await obtainWalletAddress();
} else if (!wallet && address) {
setAddressBIP21Encoded(address);
}
} catch (error) {
console.error('Error during focus effect:', error);
}
});
return () => {
@ -423,7 +436,7 @@ const ReceiveDetails = () => {
const handleShareButtonPressed = () => {
Share.open({ message: currentTab === loc.wallets.details_address ? bip21encoded : wallet.getBIP47PaymentCode() }).catch(error =>
console.debug(error),
console.debug('Error sharing:', error),
);
};

View File

@ -340,7 +340,11 @@ const Confirm: React.FC = () => {
{state.isLoading ? (
<ActivityIndicator />
) : (
<Button disabled={isElectrumDisabled || state.isButtonDisabled} onPress={handleSendTransaction} title={loc.send.confirm_sendNow} />
<Button
disabled={isElectrumDisabled || state.isButtonDisabled}
onPress={handleSendTransaction}
title={loc.send.confirm_sendNow}
/>
)}
</BlueCard>
</View>
@ -444,4 +448,4 @@ const styles = StyleSheet.create({
fontSize: 15,
fontWeight: 'bold',
},
});
});