Refactor: Enhance logging and error handling in FlyerUploader, AuthProvider, and checksum utilities for improved debugging and maintainability
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 17m58s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 17m58s
This commit is contained in:
@@ -124,51 +124,39 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
|
||||
return;
|
||||
}
|
||||
|
||||
const promises = [];
|
||||
// Create an array of promises for the API calls that need to be made.
|
||||
// Because useApi() catches errors and returns null, we can safely use Promise.all.
|
||||
const promisesToRun = [];
|
||||
if (profileDataChanged) {
|
||||
promises.push(updateProfile({ full_name: fullName, avatar_url: avatarUrl }));
|
||||
promisesToRun.push(updateProfile({ full_name: fullName, avatar_url: avatarUrl }));
|
||||
}
|
||||
if (addressDataChanged) {
|
||||
promises.push(updateAddress(address));
|
||||
promisesToRun.push(updateAddress(address));
|
||||
}
|
||||
|
||||
try {
|
||||
const results = await Promise.allSettled(promises);
|
||||
let allSucceeded = true;
|
||||
let updatedProfileData: Profile | null = null;
|
||||
const results = await Promise.all(promisesToRun);
|
||||
|
||||
results.forEach((result, index) => {
|
||||
if (result.status === 'rejected') {
|
||||
allSucceeded = false;
|
||||
// Error is already handled by useApi hook, but we log it for good measure.
|
||||
logger.error({ err: result.reason }, 'A profile save operation failed:');
|
||||
} else if (result.status === 'fulfilled') {
|
||||
// useApi returns null if an error occurred but was caught.
|
||||
// We must treat null results as failures for the purpose of 'allSucceeded'.
|
||||
if (!result.value) {
|
||||
allSucceeded = false;
|
||||
} else {
|
||||
// If this was the profile update promise, capture its result.
|
||||
// We assume the profile promise is always first if it exists.
|
||||
if (profileDataChanged && index === 0) {
|
||||
updatedProfileData = result.value as Profile;
|
||||
onProfileUpdate(updatedProfileData);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// The useApi hook returns null on failure. Check if any results are null.
|
||||
const allSucceeded = results.every(result => result !== null);
|
||||
|
||||
if (allSucceeded) {
|
||||
notifySuccess('Profile updated successfully!');
|
||||
// If the profile data was part of the update, call the onProfileUpdate callback
|
||||
// with the result, which will be the first item in the results array.
|
||||
if (profileDataChanged) {
|
||||
onProfileUpdate(results[0] as Profile);
|
||||
}
|
||||
onClose();
|
||||
} else {
|
||||
// If some succeeded, we still might want to give feedback.
|
||||
if (updatedProfileData) {
|
||||
notifySuccess('Profile details updated, but address failed to save.');
|
||||
}
|
||||
// The modal remains open for the user to correct the error.
|
||||
// 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.');
|
||||
}
|
||||
} catch (error) {
|
||||
// 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:');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user