mirror of
https://github.com/Azornes/Comfyui-LayerForge.git
synced 2026-03-21 20:52:12 -03:00
Standart Error in Utils
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { api } from "../../../scripts/api.js";
|
||||
import { createModuleLogger } from "./LoggerUtils.js";
|
||||
import { withErrorHandling, createValidationError, createNetworkError } from "../ErrorHandler.js";
|
||||
const log = createModuleLogger('ImageUploadUtils');
|
||||
/**
|
||||
* Uploads an image blob to ComfyUI server and returns image element
|
||||
@@ -7,7 +8,13 @@ const log = createModuleLogger('ImageUploadUtils');
|
||||
* @param options - Upload options
|
||||
* @returns Promise with upload result
|
||||
*/
|
||||
export async function uploadImageBlob(blob, options = {}) {
|
||||
export const uploadImageBlob = withErrorHandling(async function (blob, options = {}) {
|
||||
if (!blob) {
|
||||
throw createValidationError("Blob is required", { blob });
|
||||
}
|
||||
if (blob.size === 0) {
|
||||
throw createValidationError("Blob cannot be empty", { blobSize: blob.size });
|
||||
}
|
||||
const { filenamePrefix = 'layerforge', overwrite = true, type = 'temp', nodeId } = options;
|
||||
// Generate unique filename
|
||||
const timestamp = Date.now();
|
||||
@@ -30,9 +37,12 @@ export async function uploadImageBlob(blob, options = {}) {
|
||||
body: formData,
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = new Error(`Failed to upload image: ${response.statusText}`);
|
||||
log.error('Image upload failed:', error);
|
||||
throw error;
|
||||
throw createNetworkError(`Failed to upload image: ${response.statusText}`, {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
filename,
|
||||
blobSize: blob.size
|
||||
});
|
||||
}
|
||||
const data = await response.json();
|
||||
log.debug('Image uploaded successfully:', data);
|
||||
@@ -52,7 +62,7 @@ export async function uploadImageBlob(blob, options = {}) {
|
||||
};
|
||||
imageElement.onerror = (error) => {
|
||||
log.error("Failed to load uploaded image", error);
|
||||
reject(new Error("Failed to load uploaded image"));
|
||||
reject(createNetworkError("Failed to load uploaded image", { error, imageUrl, filename }));
|
||||
};
|
||||
imageElement.src = imageUrl;
|
||||
});
|
||||
@@ -62,14 +72,17 @@ export async function uploadImageBlob(blob, options = {}) {
|
||||
imageUrl,
|
||||
imageElement
|
||||
};
|
||||
}
|
||||
}, 'uploadImageBlob');
|
||||
/**
|
||||
* Uploads canvas content as image blob
|
||||
* @param canvas - Canvas element or Canvas object with canvasLayers
|
||||
* @param options - Upload options
|
||||
* @returns Promise with upload result
|
||||
*/
|
||||
export async function uploadCanvasAsImage(canvas, options = {}) {
|
||||
export const uploadCanvasAsImage = withErrorHandling(async function (canvas, options = {}) {
|
||||
if (!canvas) {
|
||||
throw createValidationError("Canvas is required", { canvas });
|
||||
}
|
||||
let blob = null;
|
||||
// Handle different canvas types
|
||||
if (canvas.canvasLayers && typeof canvas.canvasLayers.getFlattenedCanvasAsBlob === 'function') {
|
||||
@@ -81,26 +94,37 @@ export async function uploadCanvasAsImage(canvas, options = {}) {
|
||||
blob = await new Promise(resolve => canvas.toBlob(resolve));
|
||||
}
|
||||
else {
|
||||
throw new Error("Unsupported canvas type");
|
||||
throw createValidationError("Unsupported canvas type", {
|
||||
canvas,
|
||||
hasCanvasLayers: !!canvas.canvasLayers,
|
||||
isHTMLCanvas: canvas instanceof HTMLCanvasElement
|
||||
});
|
||||
}
|
||||
if (!blob) {
|
||||
throw new Error("Failed to generate canvas blob");
|
||||
throw createValidationError("Failed to generate canvas blob", { canvas, options });
|
||||
}
|
||||
return uploadImageBlob(blob, options);
|
||||
}
|
||||
}, 'uploadCanvasAsImage');
|
||||
/**
|
||||
* Uploads canvas with mask as image blob
|
||||
* @param canvas - Canvas object with canvasLayers
|
||||
* @param options - Upload options
|
||||
* @returns Promise with upload result
|
||||
*/
|
||||
export async function uploadCanvasWithMaskAsImage(canvas, options = {}) {
|
||||
export const uploadCanvasWithMaskAsImage = withErrorHandling(async function (canvas, options = {}) {
|
||||
if (!canvas) {
|
||||
throw createValidationError("Canvas is required", { canvas });
|
||||
}
|
||||
if (!canvas.canvasLayers || typeof canvas.canvasLayers.getFlattenedCanvasWithMaskAsBlob !== 'function') {
|
||||
throw new Error("Canvas does not support mask operations");
|
||||
throw createValidationError("Canvas does not support mask operations", {
|
||||
canvas,
|
||||
hasCanvasLayers: !!canvas.canvasLayers,
|
||||
hasMaskMethod: !!(canvas.canvasLayers && typeof canvas.canvasLayers.getFlattenedCanvasWithMaskAsBlob === 'function')
|
||||
});
|
||||
}
|
||||
const blob = await canvas.canvasLayers.getFlattenedCanvasWithMaskAsBlob();
|
||||
if (!blob) {
|
||||
throw new Error("Failed to generate canvas with mask blob");
|
||||
throw createValidationError("Failed to generate canvas with mask blob", { canvas, options });
|
||||
}
|
||||
return uploadImageBlob(blob, options);
|
||||
}
|
||||
}, 'uploadCanvasWithMaskAsImage');
|
||||
|
||||
Reference in New Issue
Block a user