mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 11:31:12 -05:00
Improve error handling and stability across the application
Refactor error handling in `useEntityVersions` and `useSearch` hooks, enhance `NotificationService` with better error extraction and logging, and implement critical fallback mechanisms in the `detect-location` function's rate limit cleanup. Update CORS configuration in `upload-image` function for stricter origin checks and better security. Replit-Commit-Author: Agent Replit-Commit-Session-Id: f4df1950-6410-48d0-b2de-f4096732504b Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
@@ -38,6 +38,16 @@ class NotificationService {
|
||||
this.isNovuEnabled = !!import.meta.env.VITE_NOVU_APPLICATION_IDENTIFIER;
|
||||
}
|
||||
|
||||
private extractErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
if (error && typeof error === 'object' && 'message' in error && typeof error.message === 'string') {
|
||||
return error.message;
|
||||
}
|
||||
return 'An unexpected error occurred';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update a Novu subscriber
|
||||
*/
|
||||
@@ -52,9 +62,17 @@ class NotificationService {
|
||||
body: subscriberData,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error('Edge function error creating Novu subscriber:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!data || !data.subscriberId) {
|
||||
const errorMsg = 'Invalid response from create-novu-subscriber function';
|
||||
console.error(errorMsg, { data });
|
||||
return { success: false, error: errorMsg };
|
||||
}
|
||||
|
||||
// Update local database with Novu subscriber ID
|
||||
const { error: dbError } = await supabase
|
||||
.from('user_notification_preferences')
|
||||
.upsert({
|
||||
@@ -62,12 +80,16 @@ class NotificationService {
|
||||
novu_subscriber_id: data.subscriberId,
|
||||
});
|
||||
|
||||
if (dbError) throw dbError;
|
||||
if (dbError) {
|
||||
console.error('Database error storing subscriber preferences:', dbError);
|
||||
throw dbError;
|
||||
}
|
||||
|
||||
console.log('Novu subscriber created successfully:', data.subscriberId);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
console.error('Error creating Novu subscriber:', error);
|
||||
return { success: false, error: error instanceof Error ? error.message : 'An unexpected error occurred' };
|
||||
return { success: false, error: this.extractErrorMessage(error) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,13 +107,16 @@ class NotificationService {
|
||||
body: subscriberData,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error('Edge function error updating Novu subscriber:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('Novu subscriber updated successfully');
|
||||
console.log('Novu subscriber updated successfully:', subscriberData.subscriberId);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
console.error('Error updating Novu subscriber:', error);
|
||||
return { success: false, error: error instanceof Error ? error.message : 'An unexpected error occurred' };
|
||||
return { success: false, error: this.extractErrorMessage(error) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +128,6 @@ class NotificationService {
|
||||
preferences: NotificationPreferences
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
if (!this.isNovuEnabled) {
|
||||
// Save to local database only
|
||||
try {
|
||||
const { error } = await supabase
|
||||
.from('user_notification_preferences')
|
||||
@@ -114,10 +138,16 @@ class NotificationService {
|
||||
frequency_settings: preferences.frequencySettings,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error('Database error saving preferences (Novu disabled):', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('Preferences saved to local database:', userId);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message };
|
||||
} catch (error: unknown) {
|
||||
console.error('Error saving preferences to local database:', error);
|
||||
return { success: false, error: this.extractErrorMessage(error) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,9 +159,11 @@ class NotificationService {
|
||||
},
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error('Edge function error updating Novu preferences:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Also update local database
|
||||
const { error: dbError } = await supabase
|
||||
.from('user_notification_preferences')
|
||||
.upsert({
|
||||
@@ -141,12 +173,16 @@ class NotificationService {
|
||||
frequency_settings: preferences.frequencySettings,
|
||||
});
|
||||
|
||||
if (dbError) throw dbError;
|
||||
if (dbError) {
|
||||
console.error('Database error saving preferences locally:', dbError);
|
||||
throw dbError;
|
||||
}
|
||||
|
||||
console.log('Preferences updated successfully:', userId);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
console.error('Error updating preferences:', error);
|
||||
return { success: false, error: error.message };
|
||||
return { success: false, error: this.extractErrorMessage(error) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,9 +200,17 @@ class NotificationService {
|
||||
body: payload,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error('Edge function error triggering notification:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!data || !data.transactionId) {
|
||||
const errorMsg = 'Invalid response from trigger-notification function';
|
||||
console.error(errorMsg, { data });
|
||||
return { success: false, error: errorMsg };
|
||||
}
|
||||
|
||||
// Log notification in local database
|
||||
await this.logNotification({
|
||||
userId: payload.subscriberId,
|
||||
workflowId: payload.workflowId,
|
||||
@@ -174,10 +218,11 @@ class NotificationService {
|
||||
payload: payload.payload,
|
||||
});
|
||||
|
||||
console.log('Notification triggered successfully:', data.transactionId);
|
||||
return { success: true, transactionId: data.transactionId };
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
console.error('Error triggering notification:', error);
|
||||
return { success: false, error: error.message };
|
||||
return { success: false, error: this.extractErrorMessage(error) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,10 +237,13 @@ class NotificationService {
|
||||
.eq('user_id', userId)
|
||||
.single();
|
||||
|
||||
if (error && error.code !== 'PGRST116') throw error;
|
||||
if (error && error.code !== 'PGRST116') {
|
||||
console.error('Database error fetching preferences:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
// Return default preferences
|
||||
console.log('No preferences found for user, returning defaults:', userId);
|
||||
return {
|
||||
channelPreferences: {
|
||||
in_app: true,
|
||||
@@ -216,8 +264,8 @@ class NotificationService {
|
||||
workflowPreferences: data.workflow_preferences as any,
|
||||
frequencySettings: data.frequency_settings as any,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching preferences:', error);
|
||||
} catch (error: unknown) {
|
||||
console.error('Error fetching notification preferences for user:', userId, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -233,10 +281,14 @@ class NotificationService {
|
||||
.eq('is_active', true)
|
||||
.order('category', { ascending: true });
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error('Database error fetching notification templates:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data || [];
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching templates:', error);
|
||||
} catch (error: unknown) {
|
||||
console.error('Error fetching notification templates:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user