Bläddra i källkod

fix: improve error handling for store deletion #28

- Enhanced backend API to return detailed error messages when store deletion fails
- Added logging for store deletion attempts and outcomes
- Updated frontend to display specific error details from backend
- Fixed error message display to show actual cause of deletion failure

This addresses the issue where users couldn't understand why store deletion was failing.
Claude 5 månader sedan
förälder
incheckning
90420aad9b

+ 7 - 2
shopcall.ai-main/src/components/IntegrationsContent.tsx

@@ -242,8 +242,12 @@ export function IntegrationsContent() {
         }
       });
 
+      const data = await response.json();
+
       if (!response.ok) {
-        throw new Error('Failed to disconnect store');
+        // Show detailed error from backend
+        const errorMessage = data.details || data.error || 'Failed to disconnect store';
+        throw new Error(errorMessage);
       }
 
       toast({
@@ -255,9 +259,10 @@ export function IntegrationsContent() {
       fetchStores();
     } catch (error) {
       console.error('Error disconnecting store:', error);
+      const errorMessage = error instanceof Error ? error.message : 'Failed to disconnect the store. Please try again.';
       toast({
         title: "Disconnection Failed",
-        description: "Failed to disconnect the store. Please try again.",
+        description: errorMessage,
         variant: "destructive",
       });
     }

+ 21 - 5
supabase/functions/api/index.ts

@@ -324,19 +324,25 @@ serve(async (req) => {
       // Verify the store belongs to the user
       const { data: store, error: fetchError } = await supabase
         .from('stores')
-        .select('id')
+        .select('id, platform_name, store_name')
         .eq('id', storeId)
         .eq('user_id', user.id)
         .single()
 
       if (fetchError || !store) {
+        console.error('Error fetching store for deletion:', fetchError)
         return new Response(
-          JSON.stringify({ error: 'Store not found or access denied' }),
+          JSON.stringify({
+            error: 'Store not found or access denied',
+            details: fetchError ? fetchError.message : 'Store does not exist or you do not have permission to delete it'
+          }),
           { status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
         )
       }
 
-      // Delete the store
+      console.log(`[API] Attempting to delete store: ${store.store_name} (${store.platform_name}) - ID: ${storeId}`)
+
+      // Delete the store - cascading will handle related records
       const { error: deleteError } = await supabase
         .from('stores')
         .delete()
@@ -344,13 +350,23 @@ serve(async (req) => {
         .eq('user_id', user.id)
 
       if (deleteError) {
-        console.error('Error deleting store:', deleteError)
+        console.error('Error deleting store:', {
+          storeId,
+          userId: user.id,
+          error: deleteError
+        })
         return new Response(
-          JSON.stringify({ error: 'Failed to delete store' }),
+          JSON.stringify({
+            error: 'Failed to delete store',
+            details: deleteError.message || 'An unknown error occurred while deleting the store',
+            code: deleteError.code || 'unknown'
+          }),
           { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
         )
       }
 
+      console.log(`[API] Store deleted successfully: ${storeId}`)
+
       return new Response(
         JSON.stringify({
           success: true,