diff --git a/supabase/functions/process-selective-approval/index.ts b/supabase/functions/process-selective-approval/index.ts index c5d3997f..e0bfe17d 100644 --- a/supabase/functions/process-selective-approval/index.ts +++ b/supabase/functions/process-selective-approval/index.ts @@ -16,7 +16,31 @@ import { import { formatEdgeError, toError } from '../_shared/errorFormatter.ts'; const SUPABASE_URL = Deno.env.get('SUPABASE_URL') || 'https://api.thrillwiki.com'; -const SUPABASE_ANON_KEY = Deno.env.get('SUPABASE_ANON_KEY')!; +const SUPABASE_ANON_KEY = Deno.env.get('SUPABASE_ANON_KEY'); + +// ============================================================================ +// CRITICAL: Validate environment variables at startup +// ============================================================================ +if (!SUPABASE_ANON_KEY) { + const errorMsg = 'CRITICAL: SUPABASE_ANON_KEY environment variable is not set!'; + console.error(errorMsg, { + timestamp: new Date().toISOString(), + hasUrl: !!SUPABASE_URL, + url: SUPABASE_URL, + availableEnvVars: Object.keys(Deno.env.toObject()).filter(k => + k.includes('SUPABASE') || k.includes('URL') + ) + }); + throw new Error('Missing required environment variable: SUPABASE_ANON_KEY'); +} + +console.log('Edge function initialized successfully', { + timestamp: new Date().toISOString(), + function: 'process-selective-approval', + hasUrl: !!SUPABASE_URL, + hasKey: !!SUPABASE_ANON_KEY, + keyLength: SUPABASE_ANON_KEY.length +}); interface ApprovalRequest { submissionId: string; @@ -26,6 +50,20 @@ interface ApprovalRequest { // Main handler function const handler = async (req: Request) => { + // ============================================================================ + // Log every incoming request immediately + // ============================================================================ + console.log('Request received', { + timestamp: new Date().toISOString(), + method: req.method, + url: req.url, + headers: { + authorization: req.headers.has('Authorization') ? '[PRESENT]' : '[MISSING]', + contentType: req.headers.get('Content-Type'), + traceparent: req.headers.get('traceparent') || '[NONE]' + } + }); + // Handle CORS preflight requests if (req.method === 'OPTIONS') { return new Response(null, { @@ -485,20 +523,29 @@ const handler = async (req: Request) => { ); } catch (error) { + // Enhanced error logging with full details + const errorDetails = { + timestamp: new Date().toISOString(), + requestId: rootSpan?.spanId || 'unknown', + duration: rootSpan?.duration || 0, + error: formatEdgeError(error), + errorType: error instanceof Error ? error.constructor.name : typeof error, + stack: error instanceof Error ? error.stack : undefined, + action: 'process_approval' + }; + + console.error('Uncaught error in handler', errorDetails); + endSpan(rootSpan, 'error', error instanceof Error ? error : toError(error)); logSpan(rootSpan); - edgeLogger.error('Unexpected error', { - requestId, - duration: rootSpan.duration, - error: formatEdgeError(error), - stack: error instanceof Error ? error.stack : undefined, - action: 'process_approval' - }); + edgeLogger.error('Unexpected error', errorDetails); + return new Response( JSON.stringify({ error: 'Internal server error', - message: error instanceof Error ? error.message : 'Unknown error' + message: error instanceof Error ? error.message : 'Unknown error', + requestId: rootSpan?.spanId || 'unknown' }), { status: 500,