0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-02-21 13:38:39 +01:00

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 <noreply@anthropic.com>
This commit is contained in:
silverwind 2026-02-20 13:40:11 +01:00
parent dbb8312ff3
commit 5e936bd3c5
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443

View File

@ -283,8 +283,10 @@ function initRepoDiffHashChangeListener() {
const expandAllSavedState = new Map<string, HTMLElement>();
async function fetchBlobExcerpt(tr: Element, url: string): Promise<void> {
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}`);
}
});
}