mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 13:11:16 -05:00
Add defensive logging to edge function
Enhance process-selective-approval with startup env var validation, request-level logging, and improved error handling to diagnose edge function failures. Includes checks for SUPABASE_ANON_KEY, startup init log, per-request logs, and robust catch-block logging.
This commit is contained in:
@@ -16,7 +16,31 @@ import {
|
|||||||
import { formatEdgeError, toError } from '../_shared/errorFormatter.ts';
|
import { formatEdgeError, toError } from '../_shared/errorFormatter.ts';
|
||||||
|
|
||||||
const SUPABASE_URL = Deno.env.get('SUPABASE_URL') || 'https://api.thrillwiki.com';
|
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 {
|
interface ApprovalRequest {
|
||||||
submissionId: string;
|
submissionId: string;
|
||||||
@@ -26,6 +50,20 @@ interface ApprovalRequest {
|
|||||||
|
|
||||||
// Main handler function
|
// Main handler function
|
||||||
const handler = async (req: Request) => {
|
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
|
// Handle CORS preflight requests
|
||||||
if (req.method === 'OPTIONS') {
|
if (req.method === 'OPTIONS') {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
@@ -485,20 +523,29 @@ const handler = async (req: Request) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
} catch (error) {
|
} 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));
|
endSpan(rootSpan, 'error', error instanceof Error ? error : toError(error));
|
||||||
logSpan(rootSpan);
|
logSpan(rootSpan);
|
||||||
|
|
||||||
edgeLogger.error('Unexpected error', {
|
edgeLogger.error('Unexpected error', errorDetails);
|
||||||
requestId,
|
|
||||||
duration: rootSpan.duration,
|
|
||||||
error: formatEdgeError(error),
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
action: 'process_approval'
|
|
||||||
});
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
error: 'Internal server error',
|
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,
|
status: 500,
|
||||||
|
|||||||
Reference in New Issue
Block a user