[object Object]

← back to 4square Admin

fix: handle fetch network failures in loadSources and loadProducts instead of leaving a stuck spinner

2c2f866bf131bfa81ef3ce7fad505b6470c2cfa4 · 2026-05-18 17:42:36 -0700 · Steve

Files touched

Diff

commit 2c2f866bf131bfa81ef3ce7fad505b6470c2cfa4
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon May 18 17:42:36 2026 -0700

    fix: handle fetch network failures in loadSources and loadProducts instead of leaving a stuck spinner
---
 index.html | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/index.html b/index.html
index f55ea3d..b7b34a7 100644
--- a/index.html
+++ b/index.html
@@ -330,7 +330,14 @@ const toast = (msg, kind='ok') => {
 const esc = s => String(s ?? '').replace(/[&<>"']/g, ch => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[ch]));
 
 async function loadSources() {
-  const j = await fetch('/api/sources').then(r => r.json());
+  let j;
+  try {
+    j = await fetch('/api/sources').then(r => r.json());
+  } catch (e) {
+    toast('Could not load sources: ' + e.message, 'err');
+    return;
+  }
+  if (!j.ok) { toast('Sources error: ' + (j.error || 'unknown'), 'err'); return; }
   SOURCES = j.sources;
   renderRail();
 }
@@ -392,7 +399,13 @@ async function loadProducts({ append = false } = {}) {
   }
   $('#bc').textContent = CURRENT.id + (q ? ` · "${q}"` : '');
   const url = `/api/products?source=${encodeURIComponent(CURRENT.id)}&sort=${encodeURIComponent(sort)}&limit=300&offset=${CURRENT.offset}${q ? `&q=${encodeURIComponent(q)}` : ''}`;
-  const j = await fetch(url).then(r => r.json());
+  let j;
+  try {
+    j = await fetch(url).then(r => r.json());
+  } catch (e) {
+    $('#grid-wrap').innerHTML = `<div class="empty-state">request failed: ${esc(e.message)}</div>`;
+    return;
+  }
   if (!j.ok) { $('#grid-wrap').innerHTML = `<div class="empty-state">error: ${esc(j.error)}</div>`; return; }
   CURRENT.kind = j.kind;
   CURRENT.rows = append ? CURRENT.rows.concat(j.rows) : j.rows;

← d04b35d fix: revoke CSV blob object URL after download to prevent me  ·  back to 4square Admin  ·  ignore backup/editor junk patterns 2537670 →