From ada2ba1062ad6d779294a258bacbc4191aad9c80 Mon Sep 17 00:00:00 2001 From: jae Date: Thu, 2 Apr 2026 01:16:09 +0000 Subject: [PATCH] fix: blog TRANSMISSION ERROR - coffee=6 caused RangeError in String.repeat(-1) - Clamp buildCoffee val to 0-5 range - Fix post data: cap coffee at 5 - Add per-card error protection in rendering - Fix fallback URL to absolute path - Show detailed error messages for debugging --- api/data/posts.json | 2 +- js/blog.js | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/api/data/posts.json b/api/data/posts.json index 77e442e..6e31d7f 100644 --- a/api/data/posts.json +++ b/api/data/posts.json @@ -18,7 +18,7 @@ "motivation": 5, "focus": 4, "difficulty": 3, - "coffee": 6, + "coffee": 5, "heart_rate": 76, "threat_level": "LOW" }, diff --git a/js/blog.js b/js/blog.js index aa1c43a..fd8185f 100644 --- a/js/blog.js +++ b/js/blog.js @@ -51,6 +51,7 @@ // ─── Coffee Icons ─── function buildCoffee(val) { + val = Math.max(0, Math.min(5, val || 0)); return '' + '☕'.repeat(val) + '' + '☕'.repeat(5 - val) + ''; } @@ -105,17 +106,28 @@ try { const res = await fetch(API + '/posts'); - if (!res.ok) throw new Error('API error'); + if (!res.ok) throw new Error('API ' + res.status); const posts = await res.json(); - if (posts.length === 0) { + if (!Array.isArray(posts) || posts.length === 0) { grid.innerHTML = '
NO TRANSMISSIONS FOUND
'; return; } // Sort by date descending posts.sort((a, b) => new Date(b.date) - new Date(a.date)); - grid.innerHTML = posts.map(renderPostCard).join(''); + + // Render each card with individual error protection + let html = ''; + posts.forEach((post, idx) => { + try { + html += renderPostCard(post); + } catch (cardErr) { + console.error('Card render error for post ' + idx + ':', cardErr); + html += '

RENDER ERROR

' + cardErr.message + '

'; + } + }); + grid.innerHTML = html; // Animate cards in const cards = grid.querySelectorAll('.post-card'); @@ -130,9 +142,11 @@ }); } catch (err) { - // Fallback: load from static JSON + console.error('Blog primary load error:', err); + // Fallback: load from static JSON (absolute path) try { - const res2 = await fetch('api/data/posts.json'); + const res2 = await fetch('/api/data/posts.json'); + if (!res2.ok) throw new Error('Fallback ' + res2.status); const posts = await res2.json(); posts.sort((a, b) => new Date(b.date) - new Date(a.date)); grid.innerHTML = posts.map(renderPostCard).join(''); @@ -147,12 +161,11 @@ }, 100 + i * 150); }); } catch (e2) { - grid.innerHTML = '
TRANSMISSION ERROR — RETRY LATER
'; + console.error('Blog fallback error:', e2); + grid.innerHTML = '
TRANSMISSION ERROR: ' + err.message + ' / FALLBACK: ' + e2.message + '
'; } } } - - // ─── Filters ─── function initFilters() { const btns = document.querySelectorAll('.filter-btn'); btns.forEach(btn => {