Skip to content

Commit cdec628

Browse files
committed
fix(upload): improve error handling and status updates for image upload
- Add status display when starting image upload - Enhance error messages with HTTP status for failed uploads - Show error status on upload failure - Add error handling and logging in dataURL to Blob conversion - Log uploaded image URL to console - Display status updates and logs when receiving plugin messages - Show raw response data when color parsing fails - Improve message logging and status updates in fallback color analysis - Provide more specific prompt structure for LLM image color analysis requests
1 parent 86b833e commit cdec628

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

js/mood-palette.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ function captureImageFromPTT() {
171171
// Function to upload image to catbox.moe
172172
async function uploadToCatbox(imageData) {
173173
try {
174+
showStatus('UPLOADING IMAGE...', 'info');
175+
174176
// Convert data URL to Blob
175177
const blob = dataURLToBlob(imageData);
176178

@@ -187,29 +189,40 @@ async function uploadToCatbox(imageData) {
187189

188190
if (response.ok) {
189191
const url = await response.text();
192+
console.log('Image uploaded to:', url);
190193
return url;
191194
} else {
192-
throw new Error('Upload failed');
195+
throw new Error('Upload failed with status: ' + response.status);
193196
}
194197
} catch (error) {
195198
console.error('Catbox upload error:', error);
199+
showStatus('UPLOAD FAILED: ' + error.message, 'error');
196200
return null;
197201
}
198202
}
199203

200204
// Helper function to convert data URL to Blob
201205
function dataURLToBlob(dataURL) {
202-
const parts = dataURL.split(';base64,');
203-
const contentType = parts[0].split(':')[1];
204-
const raw = atob(parts[1]);
205-
const rawLength = raw.length;
206-
const uInt8Array = new Uint8Array(rawLength);
207-
208-
for (let i = 0; i < rawLength; ++i) {
209-
uInt8Array[i] = raw.charCodeAt(i);
206+
try {
207+
const parts = dataURL.split(';base64,');
208+
if (parts.length !== 2) {
209+
throw new Error('Invalid data URL format');
210+
}
211+
212+
const contentType = parts[0].split(':')[1];
213+
const raw = atob(parts[1]);
214+
const rawLength = raw.length;
215+
const uInt8Array = new Uint8Array(rawLength);
216+
217+
for (let i = 0; i < rawLength; ++i) {
218+
uInt8Array[i] = raw.charCodeAt(i);
219+
}
220+
221+
return new Blob([uInt8Array], { type: contentType || 'image/jpeg' });
222+
} catch (error) {
223+
console.error('Error converting data URL to Blob:', error);
224+
throw error;
210225
}
211-
212-
return new Blob([uInt8Array], { type: contentType });
213226
}
214227

215228
function analyzeColorsFromImage() {
@@ -364,23 +377,33 @@ function resetApp() {
364377
// Plugin message handler for LLM responses
365378
window.onPluginMessage = function(data) {
366379
console.log('Received plugin message:', data);
380+
showStatus('RECEIVED AI RESPONSE', 'info');
367381

368382
if (data.data) {
369383
try {
370384
const parsedData = JSON.parse(data.data);
385+
console.log('Parsed data:', parsedData);
371386

372387
// Handle color analysis response
373388
if (parsedData.colors) {
374389
currentPalette = parsedData.colors;
375390
displayPalette(currentPalette);
376391
showStatus('PALETTE READY! EMAIL TO SEND', 'success');
392+
} else {
393+
// If we can't parse colors, show the raw data for debugging
394+
console.log('No colors found in response, showing raw data');
395+
showStatus('RESPONSE: ' + data.data, 'info');
377396
}
378397
} catch (e) {
379398
console.error('Error parsing plugin message:', e);
380-
showStatus('RECEIVED AI RESPONSE', 'info');
399+
// Show the raw data if we can't parse it
400+
showStatus('RESPONSE: ' + data.data, 'info');
381401
}
382402
} else if (data.message) {
383403
showStatus(data.message, 'info');
404+
} else {
405+
// Show raw data if no message or data
406+
showStatus('RECEIVED: ' + JSON.stringify(data), 'info');
384407
}
385408
};
386409

@@ -406,9 +429,11 @@ async function fallbackToCatboxAnalysis() {
406429

407430
if (imageUrl) {
408431
showStatus('ANALYZING COLORS...', 'info');
409-
// Send image URL to LLM for analysis
432+
console.log('Sending image URL to LLM:', imageUrl);
433+
434+
// Send image URL to LLM for analysis with a more specific prompt
410435
const payload = {
411-
message: `Analyze the colors in this image at ${imageUrl} and provide exactly 5 dominant colors in hex format. Response format: {'colors': ['#hex1', '#hex2', '#hex3', '#hex4', '#hex5']}`,
436+
message: `Please analyze the colors in this image: ${imageUrl} and provide exactly 5 dominant colors in hex format. Response format: {"colors": ["#hex1", "#hex2", "#hex3", "#hex4", "#hex5"]}`,
412437
useLLM: true
413438
};
414439

@@ -418,7 +443,7 @@ async function fallbackToCatboxAnalysis() {
418443
}
419444
} catch (error) {
420445
console.error('Fallback analysis error:', error);
421-
showStatus('ANALYSIS FAILED', 'error');
446+
showStatus('ANALYSIS FAILED: ' + error.message, 'error');
422447
}
423448
}
424449

0 commit comments

Comments
 (0)