Add comprehensive logging to handleImageUpload + fix saveField re-throw

Logging added at every step:
- handleImageUpload entry
- dimPromise resolution
- thumbPromise resolution
- Upload response status + ok flag + url
- r.json() resolution
- selectMediaItem call

Also: saveField now re-throws after logging so callers know save failed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Kunthawat Greethong
2026-05-04 13:21:58 +07:00
parent 65c876393c
commit 4ea7016f2c

View File

@@ -731,6 +731,7 @@ export function renderToolbar(config: ToolbarConfig): string {
.catch(function(err) {
setSaveState("error");
console.error("Save failed:", err);
throw err;
});
}
@@ -1149,6 +1150,7 @@ export function renderToolbar(config: ToolbarConfig): string {
}
function handleImageUpload(file, popover, annotation, element, imgEl) {
console.log("[emdash] handleImageUpload called, file:", file.name);
var collection = annotation.collection;
var id = annotation.id;
var field = annotation.field;
@@ -1180,6 +1182,7 @@ export function renderToolbar(config: ToolbarConfig): string {
});
dimPromise.then(function(dims) {
console.log("[emdash] dims resolved:", JSON.stringify(dims));
// Generate a thumbnail for large images to avoid OOM in server-side
// blurhash generation on memory-constrained runtimes (Workers).
// Thumbnail fits within a 64x64 box (scale by max dimension) so that
@@ -1227,6 +1230,7 @@ export function renderToolbar(config: ToolbarConfig): string {
}
return thumbPromise.then(function(thumbnail) {
console.log("[emdash] thumbPromise resolved, thumbnail:", thumbnail ? "yes" : "no");
var formData = new FormData();
formData.append("file", file);
if (dims.width) formData.append("width", String(dims.width));
@@ -1241,8 +1245,7 @@ export function renderToolbar(config: ToolbarConfig): string {
});
})
.then(function(r) {
console.log("[emdash] Upload response status:", r.status);
console.log("[emdash] Upload response headers:", r.headers.get("content-type"));
console.log("[emdash] Upload response status:", r.status, "ok:", r.ok, "url:", r.url);
if (!r.ok) {
return r.text().then(function(text) {
console.log("[emdash] Upload error text:", text);
@@ -1257,9 +1260,12 @@ export function renderToolbar(config: ToolbarConfig): string {
return r.json();
})
.then(function(data) {
console.log("[emdash] r.json() resolved, data:", JSON.stringify(data));
if (!data.data || !data.data.item) throw new Error("Upload failed: no item returned");
var item = data.data.item;
selectMediaItem(item, annotation, element, imgEl);
console.log("[emdash] Calling selectMediaItem with item id:", item.id);
// Return the saveField promise so rejections propagate to catch
return selectMediaItem(item, annotation, element, imgEl);
})
.catch(function(err) {
console.error("Upload error:", err);