mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 02:11:14 -05:00
Add comprehensive edge-function error handling
Enhance error handling and logging across all edge functions: - Introduce a shared edgeFunctionWrapper with standardized error handling, request/response logging, tracing, and validation hooks. - Add runtime type validation utilities (ValidationError, validators, and parse/validate helpers) and integrate into edge flow. - Implement robust validation for incoming requests and known type mismatches, with detailed logs and structured responses. - Add post-RPC and post-database error logging to surface type/mismatch issues early. - Update approval/rejection entry points to leverage new validators and centralized error handling.
This commit is contained in:
@@ -14,6 +14,11 @@ import {
|
||||
type Span
|
||||
} from '../_shared/logger.ts';
|
||||
import { formatEdgeError, toError } from '../_shared/errorFormatter.ts';
|
||||
import {
|
||||
validateRejectionRequest,
|
||||
type ValidatedRejectionRequest
|
||||
} from '../_shared/submissionValidation.ts';
|
||||
import { ValidationError } from '../_shared/typeValidation.ts';
|
||||
|
||||
const SUPABASE_URL = Deno.env.get('SUPABASE_URL') || 'https://api.thrillwiki.com';
|
||||
const SUPABASE_ANON_KEY = Deno.env.get('SUPABASE_ANON_KEY')!;
|
||||
@@ -103,10 +108,50 @@ const handler = async (req: Request) => {
|
||||
action: 'process_rejection'
|
||||
});
|
||||
|
||||
// STEP 2: Parse request
|
||||
// STEP 2: Parse and validate request
|
||||
addSpanEvent(rootSpan, 'validation_start');
|
||||
const body: RejectionRequest = await req.json();
|
||||
const { submissionId, itemIds, rejectionReason } = body;
|
||||
|
||||
let submissionId: string;
|
||||
let itemIds: string[];
|
||||
let rejectionReason: string;
|
||||
|
||||
try {
|
||||
const body = await req.json();
|
||||
const validated = validateRejectionRequest(body, requestId);
|
||||
submissionId = validated.submissionId;
|
||||
itemIds = validated.itemIds;
|
||||
rejectionReason = validated.rejectionReason;
|
||||
} catch (error) {
|
||||
if (error instanceof ValidationError) {
|
||||
addSpanEvent(rootSpan, 'validation_failed', {
|
||||
field: error.field,
|
||||
expected: error.expected,
|
||||
received: error.received,
|
||||
});
|
||||
edgeLogger.warn('Request validation failed', {
|
||||
requestId,
|
||||
field: error.field,
|
||||
expected: error.expected,
|
||||
received: error.received,
|
||||
action: 'process_rejection'
|
||||
});
|
||||
endSpan(rootSpan, 'error', error);
|
||||
logSpan(rootSpan);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: error.message,
|
||||
field: error.field,
|
||||
requestId
|
||||
}),
|
||||
{
|
||||
status: 400,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
||||
}
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
const idempotencyKey = req.headers.get('x-idempotency-key');
|
||||
|
||||
if (!idempotencyKey) {
|
||||
@@ -123,35 +168,6 @@ const handler = async (req: Request) => {
|
||||
);
|
||||
}
|
||||
|
||||
if (!submissionId || !itemIds || itemIds.length === 0 || !rejectionReason) {
|
||||
addSpanEvent(rootSpan, 'validation_failed', {
|
||||
hasSubmissionId: !!submissionId,
|
||||
hasItemIds: !!itemIds,
|
||||
itemCount: itemIds?.length || 0,
|
||||
hasReason: !!rejectionReason,
|
||||
});
|
||||
edgeLogger.warn('Invalid request payload', {
|
||||
requestId,
|
||||
hasSubmissionId: !!submissionId,
|
||||
hasItemIds: !!itemIds,
|
||||
itemCount: itemIds?.length || 0,
|
||||
hasReason: !!rejectionReason,
|
||||
action: 'process_rejection'
|
||||
});
|
||||
endSpan(rootSpan, 'error');
|
||||
logSpan(rootSpan);
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'Missing required fields: submissionId, itemIds, rejectionReason' }),
|
||||
{
|
||||
status: 400,
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
setSpanAttributes(rootSpan, {
|
||||
'submission.id': submissionId,
|
||||
'submission.item_count': itemIds.length,
|
||||
|
||||
Reference in New Issue
Block a user