Merge pull request #7200 from BlueWallet/catch

FIX: Unhandled promise rejection on GC
This commit is contained in:
GLaDOS 2024-10-26 15:58:21 +00:00 committed by GitHub
commit 15ebbd2548
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 26 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

@ -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),
);
};