mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 12:11:11 -05:00
Fix composite submission error handling
This commit is contained in:
@@ -347,6 +347,29 @@ async function submitCompositeCreation(
|
||||
depends_on: null // Will be set by RPC based on refs
|
||||
});
|
||||
|
||||
// Pre-validation to catch issues early with actionable error messages
|
||||
if (uploadedPrimary.type === 'park') {
|
||||
if (!primaryData.name) throw new Error('Park name is required');
|
||||
if (!primaryData.slug) throw new Error('Park slug is required');
|
||||
if (!primaryData.park_type) throw new Error('Park type is required');
|
||||
if (!primaryData.status) throw new Error('Park status is required');
|
||||
} else if (uploadedPrimary.type === 'ride') {
|
||||
if (!primaryData.name) throw new Error('Ride name is required');
|
||||
if (!primaryData.slug) throw new Error('Ride slug is required');
|
||||
if (!primaryData.status) throw new Error('Ride status is required');
|
||||
}
|
||||
|
||||
// Validate dependencies
|
||||
for (const dep of uploadedDependencies) {
|
||||
if (dep.type === 'company') {
|
||||
if (!dep.data.name) throw new Error(`${dep.companyType || 'Company'} name is required`);
|
||||
if (!dep.data.slug) throw new Error(`${dep.companyType || 'Company'} slug is required`);
|
||||
if (!dep.data.company_type && !dep.companyType) {
|
||||
throw new Error('Company type is required');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use RPC to create submission with items atomically
|
||||
const { data: result, error } = await supabase.rpc('create_submission_with_items', {
|
||||
p_user_id: userId,
|
||||
@@ -356,11 +379,30 @@ async function submitCompositeCreation(
|
||||
});
|
||||
|
||||
if (error) {
|
||||
handleError(error, {
|
||||
// Extract Supabase error details for better error logging
|
||||
const supabaseError = error as { message?: string; code?: string; details?: string; hint?: string };
|
||||
const errorMessage = supabaseError.message || 'Unknown error';
|
||||
const errorCode = supabaseError.code;
|
||||
const errorDetails = supabaseError.details;
|
||||
const errorHint = supabaseError.hint;
|
||||
|
||||
// Create proper Error instance with enhanced context
|
||||
const enhancedError = new Error(
|
||||
`Composite submission failed: ${errorMessage}${errorDetails ? `\nDetails: ${errorDetails}` : ''}${errorHint ? `\nHint: ${errorHint}` : ''}`
|
||||
);
|
||||
|
||||
handleError(enhancedError, {
|
||||
action: 'Composite submission',
|
||||
metadata: { primaryType: uploadedPrimary.type, dependencyCount: dependencies.length },
|
||||
metadata: {
|
||||
primaryType: uploadedPrimary.type,
|
||||
dependencyCount: dependencies.length,
|
||||
supabaseCode: errorCode,
|
||||
supabaseDetails: errorDetails,
|
||||
supabaseHint: errorHint
|
||||
},
|
||||
});
|
||||
throw new Error(`Failed to create composite submission: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
|
||||
throw enhancedError;
|
||||
}
|
||||
|
||||
return { submitted: true, submissionId: result };
|
||||
|
||||
Reference in New Issue
Block a user