Fix: Improve validation error handling

This commit is contained in:
gpt-engineer-app[bot]
2025-11-05 20:36:02 +00:00
parent c8018b827e
commit f9c11cb064

View File

@@ -837,10 +837,9 @@ serve(withRateLimit(async (req) => {
// Process items in order
for (const item of sortedItems) {
try {
edgeLogger.info('Processing item', { action: 'approval_process_item', itemId: item.id, itemType: item.item_type });
// Extract data from relational tables based on item_type
// Extract data from relational tables based on item_type (OUTSIDE try-catch)
let itemData: any;
switch (item.item_type) {
case 'park':
@@ -900,7 +899,7 @@ serve(withRateLimit(async (req) => {
});
}
// Validate entity data with strict validation, passing original_data for edits
// Validate entity data BEFORE entering try-catch (so 400 returns immediately)
const validation = validateEntityDataStrict(item.item_type, itemData, item.original_data);
if (validation.blockingErrors.length > 0) {
@@ -911,7 +910,7 @@ serve(withRateLimit(async (req) => {
requestId: tracking.requestId
});
// Fail the entire batch if ANY item has blocking errors
// Return 400 immediately - NOT caught by try-catch below
return new Response(JSON.stringify({
success: false,
message: 'Validation failed: Items have blocking errors that must be fixed',
@@ -938,6 +937,8 @@ serve(withRateLimit(async (req) => {
// Continue processing - warnings don't block approval
}
// Now enter try-catch ONLY for database operations
try {
// Set user context for versioning trigger
// This allows create_relational_version() trigger to capture the submitter
const { error: setUserIdError } = await supabase.rpc('set_config_value', {