From 16825648edd346b458d84ebb2d55da18db2c92f4 Mon Sep 17 00:00:00 2001 From: Greg Date: Sun, 18 May 2025 17:09:26 +0200 Subject: [PATCH] feat: implement Supabase client with safe query handling and error management --- myfavstuff/lib/supabaseClient.js | 65 +++++++++++++++++++------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/myfavstuff/lib/supabaseClient.js b/myfavstuff/lib/supabaseClient.js index ca6aed6..8b0d0e8 100644 --- a/myfavstuff/lib/supabaseClient.js +++ b/myfavstuff/lib/supabaseClient.js @@ -1,55 +1,66 @@ import { createClient } from '@supabase/supabase-js'; -// Function to create a supabase client that doesn't fail build if env vars are missing const createSupabaseClient = () => { - // For Coolify docker deployment with build args const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; - - // Skip errors during build - if (typeof window === 'undefined') { + + if (typeof window === 'undefined') { // Server-side or build process if (!supabaseUrl || !supabaseAnonKey) { - // During build/SSR, return null client - this prevents build errors - console.log('Building without Supabase credentials. Will connect at runtime if available.'); + console.log('Build/SSR: Supabase URL or Anon Key is missing. Client not initialized.'); return null; } - } else { - // In browser, log helpful messages but don't block rendering - if (!supabaseUrl) { - console.warn('Warning: Missing Supabase URL. Please configure NEXT_PUBLIC_SUPABASE_URL.'); + // If URL and Key are present, try to create client but catch errors during build + try { + const client = createClient(supabaseUrl, supabaseAnonKey); + // console.log('Build/SSR: Supabase client potentially initialized with provided credentials.'); // Less verbose + return client; + } catch (error) { + console.warn(`Build/SSR: Error initializing Supabase client with provided credentials: ${error.message}. This might be due to invalid credentials or network issues during build. App will attempt to connect at runtime.`); + return null; // Prevent build failure if client creation itself errors with valid-looking but problematic creds } - if (!supabaseAnonKey) { - console.warn('Warning: Missing Supabase Anon Key. Please configure NEXT_PUBLIC_SUPABASE_ANON_KEY.'); + } else { // Client-side (browser) + if (!supabaseUrl || !supabaseAnonKey) { + console.warn('Client-side: Supabase URL or Anon Key is missing. Please check environment variables.'); + return null; } - } - - // Only create client if we have credentials - if (supabaseUrl && supabaseAnonKey) { try { return createClient(supabaseUrl, supabaseAnonKey); } catch (error) { - console.error('Error initializing Supabase client:', error); + console.error(`Client-side: Error initializing Supabase client: ${error.message}`); return null; } } - - return null; }; -// Create the client (can be null during build or if credentials missing) export const supabase = createSupabaseClient(); -// Helper function for components to safely use supabase export const safeQuery = async (queryFn) => { if (!supabase) { - console.warn('Supabase client not available. Check your environment variables.'); - return { data: null, error: { message: 'Database connection not available' } }; + const buildTimeMessage = 'Build time: Supabase client not initialized for pre-rendering, due to missing or problematic credentials.'; + const runTimeMessage = 'Runtime: Supabase client not available. Check environment variables and connection.'; + + // Check if this is build time (Node.js environment during `next build`) + if (typeof window === 'undefined' && process.env.NODE_ENV === 'production') { + console.log(buildTimeMessage); + return { data: null, error: { message: 'Supabase not available during build.' } }; + } else { + console.warn(runTimeMessage); + return { data: null, error: { message: 'Supabase connection not available.' } }; + } } try { - return await queryFn(supabase); + const result = await queryFn(supabase); + // Add a check for Supabase-specific errors if the query executes but returns an error object + if (result.error) { + console.error('Supabase query error:', result.error.message); + // Return a consistent error structure + return { data: null, error: { message: result.error.message } }; + } + return result; } catch (error) { - console.error('Error in Supabase query:', error); - return { data: null, error: { message: 'Error connecting to database' } }; + // Catch unexpected errors during the queryFn execution itself + console.error('Unexpected error in Supabase query function:', error.message); + return { data: null, error: { message: `Unexpected error during database operation: ${error.message}` } }; } };