Improve resource loader error reporting and thumbnail uploads
All checks were successful
Build Org Website / build (push) Successful in 44s

This commit is contained in:
gitea-actions
2026-08-24 10:14:46 +01:00
parent 4653a2caae
commit 74924ea06f
5 changed files with 68 additions and 17 deletions

View File

@@ -107,10 +107,26 @@
els.syncStatus.querySelector("small").textContent = label;
}
async function responseError(response, prefix) {
let detail = "";
try {
const contentType = response.headers.get("Content-Type") || "";
if (contentType.includes("application/json")) {
const body = await response.json();
detail = body && body.message ? body.message : "";
} else {
detail = (await response.text()).trim().slice(0, 240);
}
} catch (_) {
detail = "";
}
return new Error(`${prefix} (${response.status})${detail ? `: ${detail}` : ""}`);
}
async function fetchServerLibrary() {
const response = await fetch(API_URL, { cache: "no-store", credentials: "same-origin" });
if (response.status === 404) return null;
if (!response.ok) throw new Error(`Server library request failed (${response.status}).`);
if (!response.ok) throw await responseError(response, "Server library request failed");
const library = Model.normaliseLibrary(await response.json());
state.serverEtag = response.headers.get("ETag");
return library;
@@ -128,7 +144,7 @@
headers: { "Content-Type": attachment.type || attachment.blob.type || "image/jpeg" },
body: attachment.blob,
});
if (!response.ok) throw new Error(`Thumbnail upload failed (${response.status}).`);
if (!response.ok) throw await responseError(response, "Thumbnail upload failed");
await deleteAttachment(id);
state.uploadedThumbnails.add(id);
}));
@@ -153,10 +169,13 @@
toast("Changes from another device were merged.");
return pushServerLibrary(merged, false);
}
if (!response.ok) throw new Error(`Server save failed (${response.status}).`);
if (!response.ok) throw await responseError(response, "Server save failed");
state.serverEtag = response.headers.get("ETag");
await uploadLocalThumbnails(library);
setSyncStatus("synced", "synced to server");
uploadLocalThumbnails(library).catch((error) => {
console.warn("Resource Loader metadata was saved, but a cover could not be uploaded.", error);
toast(`Saved to the server. Cover upload pending: ${error.message}`, true);
});
}
async function saveLibrary() {