Fix Supabase connection issues with lazy initialization and enhanced logging

This commit is contained in:
Marco Gallegos
2026-01-18 09:15:26 -06:00
parent 0b13b991c9
commit a6902b6b46
5 changed files with 271 additions and 117 deletions

View File

@@ -6,11 +6,24 @@ import { supabase } from '@/lib/supabase/client'
*/
export async function GET(request: NextRequest) {
try {
console.log('=== LOCATIONS API START ===')
console.log('Locations API called with URL:', request.url)
// Check Supabase connection
console.log('Supabase URL:', process.env.NEXT_PUBLIC_SUPABASE_URL)
console.log('Supabase key exists:', !!process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY)
// Test basic fetch to Supabase URL
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
console.log('Testing basic connectivity to Supabase...')
try {
const testResponse = await fetch(`${supabaseUrl}/rest/v1/`, {
method: 'GET',
headers: {
'apikey': process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || '',
'Content-Type': 'application/json'
}
})
console.log('Basic Supabase connectivity test:', testResponse.status, testResponse.statusText)
} catch (fetchError) {
console.error('Basic fetch test failed:', fetchError)
}
console.log('Executing locations query...')
const { data: locationsData, error: queryError } = await supabase
@@ -19,7 +32,7 @@ export async function GET(request: NextRequest) {
.eq('is_active', true)
.order('name', { ascending: true })
console.log('Query result - data:', !!locationsData, 'error:', !!queryError)
console.log('Query result - data exists:', !!locationsData, 'error exists:', !!queryError)
if (queryError) {
console.error('Locations GET error details:', {
@@ -32,25 +45,31 @@ export async function GET(request: NextRequest) {
{
error: queryError.message,
code: queryError.code,
details: queryError.details
details: queryError.details,
timestamp: new Date().toISOString()
},
{ status: 500 }
)
}
console.log('Locations found:', locationsData?.length || 0)
console.log('=== LOCATIONS API END ===')
return NextResponse.json({
success: true,
locations: locationsData || [],
count: locationsData?.length || 0
count: locationsData?.length || 0,
timestamp: new Date().toISOString()
})
} catch (error) {
console.error('=== LOCATIONS API ERROR ===')
console.error('Locations GET unexpected error:', error)
console.error('Error stack:', error instanceof Error ? error.stack : 'Unknown error')
console.error('Error type:', error instanceof Error ? error.constructor.name : typeof error)
return NextResponse.json(
{
error: 'Internal server error',
details: error instanceof Error ? error.message : 'Unknown error'
details: error instanceof Error ? error.message : 'Unknown error',
timestamp: new Date().toISOString()
},
{ status: 500 }
)