fix(backend): improve OFF API error handling
Run Tests on Branches / Detect Changes (push) Successful in 9s
Run Tests on Branches / Frontend Tests (push) Has been cancelled
Run Tests on Branches / Frontend Mobile Tests (push) Has been cancelled
Run Tests on Branches / Backend Tests (push) Has started running

- Validate response is JSON before parsing (OFF returns HTML on 503)
- Don't cache empty results to avoid blocking future searches
- Add validateStatus to only accept 200 responses
- Better logging for API failures
This commit is contained in:
Antoni Nuñez Romeu
2026-07-13 16:23:07 +02:00
parent 26f309acfb
commit ee23f61057
+12 -4
View File
@@ -62,23 +62,31 @@ export async function searchBabyProducts(query) {
action: 'process', action: 'process',
}, },
timeout: 10000, timeout: 10000,
validateStatus: (status) => status === 200,
}); });
if (response.data && response.data.products) { // Check if response is actually JSON (OFF sometimes returns HTML on error)
if (typeof response.data === 'string' || !response.data.products) {
console.warn(`[OFF] Invalid response for "${searchTerm}": API may be down`);
return [];
}
const products = response.data.products const products = response.data.products
.map(transformOFFProduct) .map(transformOFFProduct)
.filter(Boolean); .filter(Boolean);
// Only cache non-empty results to avoid caching API failures
if (products.length > 0) {
try { try {
await redisClient.setEx(cacheKey, CACHE_TTL, JSON.stringify(products)); await redisClient.setEx(cacheKey, CACHE_TTL, JSON.stringify(products));
console.log(`Cached ${products.length} OFF products for: ${searchTerm}`); console.log(`Cached ${products.length} OFF products for: ${searchTerm}`);
} catch (cacheErr) { } catch (cacheErr) {
// cache write failed, still return the data // cache write failed, still return the data
} }
return products; } else {
console.log(`[OFF] No products found for "${searchTerm}", not caching empty result`);
} }
return products;
return [];
} catch (error) { } catch (error) {
console.error('Error searching baby products from OFF:', error.message); console.error('Error searching baby products from OFF:', error.message);
return []; return [];