diff --git a/src/components/SlugInput.astro b/src/components/SlugInput.astro index 3a5b5d3..c20b255 100644 --- a/src/components/SlugInput.astro +++ b/src/components/SlugInput.astro @@ -476,17 +476,51 @@ const languages = [ `; const response = await fetch(`/api/feed/${feedId}`); - if (!response.ok) throw new Error('Failed to fetch feed'); + if (!response.ok) { + const errorText = await response.text(); + console.error(`Feed import failed with status ${response.status}: ${errorText}`); + throw new Error(`Failed to fetch feed: ${response.status} ${response.statusText}`); + } - const { slugs: feedSlugs, lang } = await response.json(); + const data = await response.json(); + console.log('Feed import data:', data); // Diagnostic log + + // Validate response structure + if (!data || typeof data !== 'object') { + throw new Error('Invalid feed data format: not an object'); + } - // Update language if provided - if (lang && langSelect) { - langSelect.value = lang; + const feedSlugs = data.slugs; + const lang = data.lang; + + // Validate slugs array + if (!feedSlugs || !Array.isArray(feedSlugs)) { + throw new Error('Invalid feed data: missing or invalid slugs array'); + } + + // Validate language + if (!lang || typeof lang !== 'string') { + console.warn('Feed has missing or invalid language, using default'); + } else if (langSelect) { + // Only update language if it's valid + const validLanguages = Array.from(langSelect.options).map(opt => opt.value); + if (validLanguages.includes(lang)) { + langSelect.value = lang; + } else { + console.warn(`Feed has unsupported language: ${lang}`); + } } // Add new slugs to existing collection - feedSlugs.forEach((slug: string) => slugs.add(slug)); + let importCount = 0; + feedSlugs.forEach((slug: string) => { + if (typeof slug === 'string' && slug.trim()) { + slugs.add(slug.trim()); + importCount++; + } else { + console.warn('Skipping invalid slug in feed:', slug); + } + }); // Update UI updateSlugs(); @@ -495,7 +529,7 @@ const languages = [ // Show success message const successDiv = document.createElement('div'); successDiv.className = 'text-sm text-green-500 mt-1'; - successDiv.textContent = 'Feed imported successfully!'; + successDiv.textContent = `Feed imported successfully! Added ${importCount} comics.`; feedIdInput.parentNode?.appendChild(successDiv); setTimeout(() => successDiv.remove(), 3000); @@ -503,9 +537,11 @@ const languages = [ console.error('Failed to import feed:', error); const errorDiv = document.createElement('div'); errorDiv.className = 'text-sm text-red-500 mt-1'; - errorDiv.textContent = 'Failed to import feed. Please check the feed ID and try again.'; + errorDiv.textContent = error instanceof Error + ? `Import failed: ${error.message}` + : 'Failed to import feed. Please check the feed ID and try again.'; feedIdInput.parentNode?.appendChild(errorDiv); - setTimeout(() => errorDiv.remove(), 3000); + setTimeout(() => errorDiv.remove(), 5000); } finally { importFeedButton.disabled = false; importFeedButton.innerHTML = ` diff --git a/src/pages/api/feed/[feedId].ts b/src/pages/api/feed/[feedId].ts index 2fe7c8f..bf7a29d 100644 --- a/src/pages/api/feed/[feedId].ts +++ b/src/pages/api/feed/[feedId].ts @@ -9,20 +9,43 @@ export const GET: APIRoute = async ({ params }) => { } try { + console.log(`Fetching feed mapping for ID: ${feedId}`); const mapping = await getFeedMapping(feedId); if (!mapping) { - return new Response("Feed not found", { status: 404 }); + console.error(`Feed not found for ID: ${feedId}`); + return new Response(`Feed with ID "${feedId}" not found`, { status: 404 }); } - return new Response(JSON.stringify(mapping), { + // Validate mapping structure before returning + if (!Array.isArray(mapping.slugs)) { + console.error(`Invalid slugs in feed mapping for ID: ${feedId}`, mapping); + return new Response(`Feed data corruption: invalid slugs format`, { + status: 500 + }); + } + + // Ensure consistent response format + const response = { + slugs: mapping.slugs.filter(slug => typeof slug === 'string' && slug.trim()), + lang: typeof mapping.lang === 'string' ? mapping.lang : 'en' + }; + + console.log(`Successfully retrieved feed mapping for ID: ${feedId}`, response); + + return new Response(JSON.stringify(response), { status: 200, headers: { 'Content-Type': 'application/json' } }); } catch (error) { - console.error('Error fetching feed:', error); - return new Response("Internal server error", { status: 500 }); + console.error(`Error fetching feed ${feedId}:`, error); + + const errorMessage = error instanceof Error + ? `Internal server error: ${error.message}` + : "Internal server error"; + + return new Response(errorMessage, { status: 500 }); } }; \ No newline at end of file