Spaces:
Running
Running
fix: add robust API error handling for data.choices undefined
Browse files- src/aiService.js +13 -0
- src/conversationManager.js +2 -1
src/aiService.js
CHANGED
|
@@ -59,6 +59,19 @@ class OpenRouterService {
|
|
| 59 |
}
|
| 60 |
|
| 61 |
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return data.choices[0].message.content.trim();
|
| 63 |
} catch (error) {
|
| 64 |
console.error('Error generating contextual hint:', error);
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
const data = await response.json();
|
| 62 |
+
|
| 63 |
+
// Check if data and choices exist before accessing
|
| 64 |
+
if (!data || !data.choices || data.choices.length === 0) {
|
| 65 |
+
console.error('Invalid API response structure:', data);
|
| 66 |
+
return 'Unable to generate hint at this time';
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// Check if message content exists
|
| 70 |
+
if (!data.choices[0].message || !data.choices[0].message.content) {
|
| 71 |
+
console.error('No content in API response');
|
| 72 |
+
return 'Unable to generate hint at this time';
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
return data.choices[0].message.content.trim();
|
| 76 |
} catch (error) {
|
| 77 |
console.error('Error generating contextual hint:', error);
|
src/conversationManager.js
CHANGED
|
@@ -95,7 +95,8 @@ class ChatService {
|
|
| 95 |
}
|
| 96 |
|
| 97 |
// Fallback - return enhanced fallback response without revealing word
|
| 98 |
-
|
|
|
|
| 99 |
}
|
| 100 |
|
| 101 |
// Build focused prompt for specific question types with level awareness
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
// Fallback - return enhanced fallback response without revealing word
|
| 98 |
+
const fallback = this.getSimpleFallback(context, questionType);
|
| 99 |
+
return fallback.response;
|
| 100 |
}
|
| 101 |
|
| 102 |
// Build focused prompt for specific question types with level awareness
|