From 5e936bd3c5561574a7aca1e6852f045b546c07f5 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 20 Feb 2026 13:40:11 +0100 Subject: [PATCH] Add error handling to blob excerpt fetch operations Check response.ok before processing fetch results in both single and batch blob excerpt expansion. Show error toasts on failure and preserve the original DOM state instead of injecting error markup. Co-Authored-By: Claude Opus 4.6 --- web_src/js/features/repo-diff.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/web_src/js/features/repo-diff.ts b/web_src/js/features/repo-diff.ts index 476ee8fc44..0bc082d8af 100644 --- a/web_src/js/features/repo-diff.ts +++ b/web_src/js/features/repo-diff.ts @@ -283,8 +283,10 @@ function initRepoDiffHashChangeListener() { const expandAllSavedState = new Map(); async function fetchBlobExcerpt(tr: Element, url: string): Promise { + const response = await GET(url); + if (!response.ok) throw new Error(`Failed to fetch blob excerpt: ${response.status}`); const tempTbody = document.createElement('tbody'); - tempTbody.innerHTML = await (await GET(url)).text(); + tempTbody.innerHTML = await response.text(); tr.replaceWith(...tempTbody.children); } @@ -316,13 +318,19 @@ async function expandAllLines(btn: HTMLElement, fileBox: HTMLElement) { } batchParams.set('direction', 'full'); - const htmlArray: string[] = await (await GET(`${parsed[0].pathname}?${batchParams}`)).json(); + const response = await GET(`${parsed[0].pathname}?${batchParams}`); + if (!response.ok) throw new Error(`Failed to fetch blob excerpts: ${response.status}`); + const htmlArray: string[] = await response.json(); for (const [index, html] of htmlArray.entries()) { const tempTbody = document.createElement('tbody'); tempTbody.innerHTML = html; expanders[index].tr.replaceWith(...tempTbody.children); } } + } catch (err) { + expandAllSavedState.delete(fileBox.id); + showErrorToast(`Failed to expand: ${err.message}`); + return; } finally { btn.classList.remove('disabled'); } @@ -385,8 +393,12 @@ function initDiffExpandAllLines() { } }); - registerGlobalEventFunc('click', 'onExpanderButtonClick', (btn: HTMLElement) => { - fetchBlobExcerpt(btn.closest('tr')!, btn.getAttribute('data-url')!); + registerGlobalEventFunc('click', 'onExpanderButtonClick', async (btn: HTMLElement) => { + try { + await fetchBlobExcerpt(btn.closest('tr')!, btn.getAttribute('data-url')!); + } catch (err) { + showErrorToast(`Failed to expand: ${err.message}`); + } }); }