Refactor: Enhance logging and error handling in FlyerUploader and ProfileManager tests for improved clarity and robustness
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 23m21s

This commit is contained in:
2025-12-16 10:42:12 -08:00
parent 88f856c141
commit 9f9aa884ea
4 changed files with 105 additions and 29 deletions

View File

@@ -77,24 +77,32 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
const { execute: fetchAddress } = useApi<Address, [number]>(fetchAddressWrapper);
const handleAddressFetch = useCallback(async (addressId: number) => {
logger.debug(`[handleAddressFetch] Starting fetch for addressId: ${addressId}`);
const fetchedAddress = await fetchAddress(addressId);
if (fetchedAddress) {
logger.debug('[handleAddressFetch] Successfully fetched address:', fetchedAddress);
setAddress(fetchedAddress);
setInitialAddress(fetchedAddress); // Set initial address on fetch
} else {
logger.warn(`[handleAddressFetch] Fetch returned null or undefined for addressId: ${addressId}. This might indicate a network error caught by useApi.`);
}
}, [fetchAddress]);
useEffect(() => {
// Only reset state when the modal is opened.
// Do not reset on profile changes, which can happen during sign-out.
logger.debug('[useEffect] Running effect due to change in isOpen or profile.', { isOpen, profileExists: !!profile });
if (isOpen && profile) { // Ensure profile exists before setting state
logger.debug('[useEffect] Modal is open with a valid profile. Resetting component state.');
setFullName(profile?.full_name || '');
setAvatarUrl(profile?.avatar_url || '');
// If the user has an address, fetch its details
if (profile.address_id) {
logger.debug(`[useEffect] Profile has address_id: ${profile.address_id}. Calling handleAddressFetch.`);
handleAddressFetch(profile.address_id);
} else {
// Reset address form if user has no address
logger.debug('[useEffect] Profile has no address_id. Resetting address form.');
setAddress({});
setInitialAddress({});
}
@@ -102,6 +110,7 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
setIsConfirmingDelete(false);
setPasswordForDelete('');
} else {
logger.debug('[useEffect] Modal is closed or profile is null. Resetting address state only.');
setAddress({});
setInitialAddress({});
}
@@ -171,16 +180,11 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
}
onClose();
} else {
// A failure occurred. The specific error notification has already been
// displayed by the failed useApi hook. We simply log it and keep the
// modal open for the user to correct any issues.
// A failure occurred. The specific error notification has already been displayed by the failed useApi hook. We simply log it and keep the modal open for the user to correct any issues.
logger.warn('handleProfileSave completed with one or more failures.');
logger.warn('[handleProfileSave] One or more operations failed. The useApi hook should have shown an error notification.');
}
} catch (error) {
// This catch block is a safeguard. In normal operation, the useApi hook
// should prevent any promises from rejecting.
// This catch block is a safeguard. In normal operation, the useApi hook should prevent any promises from rejecting.
logger.error({ err: error }, 'An unexpected error occurred in handleProfileSave:');
}
};