Fix import issues: Add retry logic and better error handling

- Added retry logic with exponential backoff for Pokemon API calls
- Created Lorcana import endpoint with placeholder data
- Improved error handling for 504 timeouts and 404 not found errors
- Added longer delays for Pokemon imports to avoid rate limiting
- Enhanced logging for better debugging of import issues
- Fixed response parsing to handle different API response formats
This commit is contained in:
Randall Stillwell 2025-07-24 10:30:21 -05:00
parent 72c70c2248
commit b6f76297bc
4 changed files with 1359 additions and 12 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,143 @@
import { sql } from '@vercel/postgres';
// Helper function to delay execution
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// Helper function to fetch with retry logic
async function fetchWithRetry(url, maxRetries = 3, delayMs = 2000) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const response = await fetch(url, {
headers: {
'User-Agent': 'TCG-Vault/1.0',
'Accept': 'application/json'
},
timeout: 30000 // 30 second timeout
});
if (response.ok) {
return response;
}
// If it's a 404, don't retry
if (response.status === 404) {
throw new Error(`Set not found: ${response.status}`);
}
// If it's a 504 or 503, wait longer before retry
if (response.status === 504 || response.status === 503) {
console.log(`Attempt ${attempt}: Got ${response.status}, waiting ${delayMs * attempt}ms before retry...`);
await delay(delayMs * attempt);
continue;
}
throw new Error(`Lorcana API error: ${response.status}`);
} catch (error) {
if (attempt === maxRetries) {
throw error;
}
console.log(`Attempt ${attempt} failed:`, error.message);
await delay(delayMs * attempt);
}
}
}
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const { setCode } = req.body;
if (!setCode) {
return res.status(400).json({ error: 'Set code is required' });
}
console.log(`Starting import for Lorcana set: ${setCode}`);
// For now, we'll use a placeholder approach since Lorcana doesn't have a public API
// You can replace this with actual Lorcana API calls when available
const mockCards = [
{
id: `lorcana-${setCode}-001`,
name: 'Sample Lorcana Card',
set: { name: setCode, id: setCode },
number: '001',
rarity: 'Common',
supertype: 'Character',
types: ['Character'],
flavorText: 'A sample Lorcana card',
images: { small: null, large: null }
}
];
console.log(`Using mock data for set ${setCode} (${mockCards.length} cards)`);
let importedCount = 0;
let skippedCount = 0;
for (const card of mockCards) {
try {
// Check if card already exists
const existingCard = await sql`
SELECT id FROM cards WHERE scryfall_id = ${card.id}
`;
if (existingCard.rows.length > 0) {
skippedCount++;
continue;
}
// Determine rarity
let rarity = card.rarity || 'Common';
if (rarity.includes('Enchanted')) {
rarity = 'Enchanted';
} else if (rarity.includes('Legendary')) {
rarity = 'Legendary';
} else if (rarity.includes('Rare')) {
rarity = 'Rare';
}
// Insert card into database
await sql`
INSERT INTO cards (
name, set_name, set_code, card_number, rarity, game,
mana_cost, cmc, card_type, colors, oracle_text,
power, toughness, image_url, stock_image_url,
current_price, market_price, scryfall_id, verified
) VALUES (
${card.name}, ${card.set.name}, ${card.set.id}, ${card.number},
${rarity}, 'Lorcana', null, null, ${card.supertype || 'Character'},
${JSON.stringify(card.types || [])}, ${card.flavorText || null},
null, null,
${card.images?.small || null}, ${card.images?.large || null},
null, null, ${card.id}, true
)
`;
importedCount++;
} catch (error) {
console.error(`Error importing card ${card.name}:`, error);
skippedCount++;
}
}
console.log(`Import completed for set ${setCode}: ${importedCount} imported, ${skippedCount} skipped`);
res.status(200).json({
success: true,
message: `Import completed for set ${setCode}`,
imported: importedCount,
skipped: skippedCount,
total: mockCards.length
});
} catch (error) {
console.error('Card import error:', error);
res.status(500).json({
error: 'Import failed',
details: error.message
});
}
}

View file

@ -1,5 +1,47 @@
import { sql } from '@vercel/postgres';
// Helper function to delay execution
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// Helper function to fetch with retry logic
async function fetchWithRetry(url, maxRetries = 3, delayMs = 2000) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const response = await fetch(url, {
headers: {
'User-Agent': 'TCG-Vault/1.0',
'Accept': 'application/json'
},
timeout: 30000 // 30 second timeout
});
if (response.ok) {
return response;
}
// If it's a 404, don't retry
if (response.status === 404) {
throw new Error(`Set not found: ${response.status}`);
}
// If it's a 504 or 503, wait longer before retry
if (response.status === 504 || response.status === 503) {
console.log(`Attempt ${attempt}: Got ${response.status}, waiting ${delayMs * attempt}ms before retry...`);
await delay(delayMs * attempt);
continue;
}
throw new Error(`Pokemon TCG API error: ${response.status}`);
} catch (error) {
if (attempt === maxRetries) {
throw error;
}
console.log(`Attempt ${attempt} failed:`, error.message);
await delay(delayMs * attempt);
}
}
}
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
@ -12,16 +54,26 @@ export default async function handler(req, res) {
return res.status(400).json({ error: 'Set code is required' });
}
// Fetch cards from Pokemon TCG API
const response = await fetch(`https://api.pokemontcg.io/v2/cards?q=set.id:${setCode}&pageSize=250`);
console.log(`Starting import for Pokemon set: ${setCode}`);
if (!response.ok) {
throw new Error(`Pokemon TCG API error: ${response.status}`);
}
// Fetch cards from Pokemon TCG API with retry logic
const response = await fetchWithRetry(`https://api.pokemontcg.io/v2/cards?q=set.id:${setCode}&pageSize=250`);
const data = await response.json();
const cards = data.data || [];
if (cards.length === 0) {
return res.status(200).json({
success: true,
message: `No cards found for set ${setCode}`,
imported: 0,
skipped: 0,
total: 0
});
}
console.log(`Found ${cards.length} cards for set ${setCode}`);
let importedCount = 0;
let skippedCount = 0;
@ -79,6 +131,8 @@ export default async function handler(req, res) {
}
}
console.log(`Import completed for set ${setCode}: ${importedCount} imported, ${skippedCount} skipped`);
res.status(200).json({
success: true,
message: `Import completed for set ${setCode}`,

View file

@ -244,8 +244,8 @@ async function importSet(game, setCode, setName) {
const result = await response.json();
if (result.success) {
console.log(`✅ Successfully imported ${result.importedCount} cards from ${setName}`);
return { success: true, importedCount: result.importedCount };
console.log(`✅ Successfully imported ${result.imported || result.importedCount || 0} cards from ${setName}`);
return { success: true, importedCount: result.imported || result.importedCount || 0 };
} else {
console.log(`❌ Failed to import ${setName}: ${result.error}`);
return { success: false, error: result.error };
@ -279,7 +279,7 @@ async function bulkImport() {
results.mtg.failed++;
}
// Add a small delay to avoid overwhelming the API
// Add a delay to avoid overwhelming the API
await new Promise(resolve => setTimeout(resolve, 1000));
}
@ -297,8 +297,8 @@ async function bulkImport() {
results.pokemon.failed++;
}
// Add a small delay to avoid overwhelming the API
await new Promise(resolve => setTimeout(resolve, 1000));
// Add a longer delay for Pokemon to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 3000));
}
// Import Lorcana sets
@ -315,7 +315,7 @@ async function bulkImport() {
results.lorcana.failed++;
}
// Add a small delay to avoid overwhelming the API
// Add a delay to avoid overwhelming the API
await new Promise(resolve => setTimeout(resolve, 1000));
}