call-ai 0.7.0-dev-preview-12 → 0.7.0-dev-preview-13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +42 -6
- package/package.json +1 -1
package/dist/api.js
CHANGED
|
@@ -105,10 +105,29 @@ function callAI(prompt, options = {}) {
|
|
|
105
105
|
const errorJson = JSON.parse(errorBody);
|
|
106
106
|
console.log(`[callAI:${PACKAGE_VERSION}] Parsed error:`, errorJson);
|
|
107
107
|
// Extract message from OpenRouter error format
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
108
|
+
let errorMessage = '';
|
|
109
|
+
// Handle common error formats
|
|
110
|
+
if (errorJson.error && typeof errorJson.error === 'object' && errorJson.error.message) {
|
|
111
|
+
// OpenRouter/OpenAI format: { error: { message: "..." } }
|
|
112
|
+
errorMessage = errorJson.error.message;
|
|
113
|
+
}
|
|
114
|
+
else if (errorJson.error && typeof errorJson.error === 'string') {
|
|
115
|
+
// Simple error format: { error: "..." }
|
|
116
|
+
errorMessage = errorJson.error;
|
|
117
|
+
}
|
|
118
|
+
else if (errorJson.message) {
|
|
119
|
+
// Generic format: { message: "..." }
|
|
120
|
+
errorMessage = errorJson.message;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Fallback with status details
|
|
124
|
+
errorMessage = `API returned ${response.status}: ${response.statusText}`;
|
|
125
|
+
}
|
|
126
|
+
// Add status details to error message if not already included
|
|
127
|
+
if (!errorMessage.includes(response.status.toString())) {
|
|
128
|
+
errorMessage = `${errorMessage} (Status: ${response.status})`;
|
|
129
|
+
}
|
|
130
|
+
console.log(`[callAI:${PACKAGE_VERSION}] Extracted error message:`, errorMessage);
|
|
112
131
|
// Create error with standard format
|
|
113
132
|
const error = new Error(errorMessage);
|
|
114
133
|
// Add useful metadata
|
|
@@ -119,9 +138,26 @@ function callAI(prompt, options = {}) {
|
|
|
119
138
|
throw error;
|
|
120
139
|
}
|
|
121
140
|
catch (jsonError) {
|
|
122
|
-
// If JSON parsing fails,
|
|
141
|
+
// If JSON parsing fails, extract a useful message from the raw error body
|
|
123
142
|
console.log(`[callAI:${PACKAGE_VERSION}] JSON parse error:`, jsonError);
|
|
124
|
-
|
|
143
|
+
// Try to extract a useful message even from non-JSON text
|
|
144
|
+
let errorMessage = '';
|
|
145
|
+
// Check if it's a plain text error message
|
|
146
|
+
if (errorBody && errorBody.trim().length > 0) {
|
|
147
|
+
// Limit length for readability
|
|
148
|
+
errorMessage = errorBody.length > 100 ?
|
|
149
|
+
errorBody.substring(0, 100) + '...' :
|
|
150
|
+
errorBody;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
errorMessage = `API error: ${response.status} ${response.statusText}`;
|
|
154
|
+
}
|
|
155
|
+
// Add status details if not already included
|
|
156
|
+
if (!errorMessage.includes(response.status.toString())) {
|
|
157
|
+
errorMessage = `${errorMessage} (Status: ${response.status})`;
|
|
158
|
+
}
|
|
159
|
+
console.log(`[callAI:${PACKAGE_VERSION}] Extracted text error message:`, errorMessage);
|
|
160
|
+
const error = new Error(errorMessage);
|
|
125
161
|
error.status = response.status;
|
|
126
162
|
error.statusText = response.statusText;
|
|
127
163
|
error.details = errorBody;
|
package/package.json
CHANGED