Skip to content

Commit e659a88

Browse files
committed
Improve API error handling with detailed error messages
- Added better error parsing to show specific API error messages - Added stream: false parameter to prevent streaming issues - Enhanced error logging for debugging - Better response validation
1 parent 600c192 commit e659a88

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

script.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,19 +444,28 @@ class AIEnhancer {
444444
}
445445
],
446446
temperature: 0.7,
447-
max_tokens: 2000
447+
max_tokens: 2000,
448+
stream: false
448449
})
449450
});
450451

451452
if (!response.ok) {
452-
throw new Error(`API Error: ${response.status} ${response.statusText}`);
453+
const errorData = await response.json().catch(() => ({}));
454+
const errorMessage = errorData.error?.message || errorData.message || response.statusText;
455+
console.error('API Error Details:', errorData);
456+
throw new Error(`API Error (${response.status}): ${errorMessage}`);
453457
}
454458

455459
const data = await response.json();
460+
461+
if (!data.choices || !data.choices[0] || !data.choices[0].message) {
462+
throw new Error('Invalid response format from API');
463+
}
464+
456465
return data.choices[0].message.content.trim();
457466
} catch (error) {
458467
console.error('AI Enhancement Error:', error);
459-
throw new Error(`AI Enhancement failed: ${error.message}`);
468+
throw error;
460469
}
461470
}
462471

0 commit comments

Comments
 (0)