Spaces:
Running
Running
Add better error handling for custom endpoint
Browse files- Add detailed error logging for fetch failures
- Catch and report connection errors
- Show HTTP status codes and error messages
- Log endpoint URL for debugging
- app/api/ask-ai/route.ts +16 -2
app/api/ask-ai/route.ts
CHANGED
|
@@ -146,14 +146,21 @@ export async function POST(request: NextRequest) {
|
|
| 146 |
headers["Authorization"] = `Bearer ${customApiKey}`;
|
| 147 |
}
|
| 148 |
|
|
|
|
|
|
|
| 149 |
const apiResponse = await fetch(`${customEndpoint}/chat/completions`, {
|
| 150 |
method: "POST",
|
| 151 |
headers,
|
| 152 |
body: JSON.stringify(requestBody),
|
|
|
|
|
|
|
|
|
|
| 153 |
});
|
| 154 |
|
| 155 |
if (!apiResponse.ok) {
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
}
|
| 158 |
|
| 159 |
const reader = apiResponse.body?.getReader();
|
|
@@ -414,14 +421,21 @@ export async function PUT(request: NextRequest) {
|
|
| 414 |
headers["Authorization"] = `Bearer ${customApiKey}`;
|
| 415 |
}
|
| 416 |
|
|
|
|
|
|
|
| 417 |
const apiResponse = await fetch(`${customEndpoint}/chat/completions`, {
|
| 418 |
method: "POST",
|
| 419 |
headers,
|
| 420 |
body: JSON.stringify(requestBody),
|
|
|
|
|
|
|
|
|
|
| 421 |
});
|
| 422 |
|
| 423 |
if (!apiResponse.ok) {
|
| 424 |
-
|
|
|
|
|
|
|
| 425 |
}
|
| 426 |
|
| 427 |
const data = await apiResponse.json();
|
|
|
|
| 146 |
headers["Authorization"] = `Bearer ${customApiKey}`;
|
| 147 |
}
|
| 148 |
|
| 149 |
+
console.log("Fetching from custom endpoint:", customEndpoint);
|
| 150 |
+
|
| 151 |
const apiResponse = await fetch(`${customEndpoint}/chat/completions`, {
|
| 152 |
method: "POST",
|
| 153 |
headers,
|
| 154 |
body: JSON.stringify(requestBody),
|
| 155 |
+
}).catch((err) => {
|
| 156 |
+
console.error("Fetch error:", err);
|
| 157 |
+
throw new Error(`Failed to connect to custom endpoint: ${err.message}`);
|
| 158 |
});
|
| 159 |
|
| 160 |
if (!apiResponse.ok) {
|
| 161 |
+
const errorText = await apiResponse.text().catch(() => "Unknown error");
|
| 162 |
+
console.error("API response error:", apiResponse.status, errorText);
|
| 163 |
+
throw new Error(`Custom endpoint returned ${apiResponse.status}: ${errorText}`);
|
| 164 |
}
|
| 165 |
|
| 166 |
const reader = apiResponse.body?.getReader();
|
|
|
|
| 421 |
headers["Authorization"] = `Bearer ${customApiKey}`;
|
| 422 |
}
|
| 423 |
|
| 424 |
+
console.log("Fetching from custom endpoint (PUT):", customEndpoint);
|
| 425 |
+
|
| 426 |
const apiResponse = await fetch(`${customEndpoint}/chat/completions`, {
|
| 427 |
method: "POST",
|
| 428 |
headers,
|
| 429 |
body: JSON.stringify(requestBody),
|
| 430 |
+
}).catch((err) => {
|
| 431 |
+
console.error("Fetch error (PUT):", err);
|
| 432 |
+
throw new Error(`Failed to connect to custom endpoint: ${err.message}`);
|
| 433 |
});
|
| 434 |
|
| 435 |
if (!apiResponse.ok) {
|
| 436 |
+
const errorText = await apiResponse.text().catch(() => "Unknown error");
|
| 437 |
+
console.error("API response error (PUT):", apiResponse.status, errorText);
|
| 438 |
+
throw new Error(`Custom endpoint returned ${apiResponse.status}: ${errorText}`);
|
| 439 |
}
|
| 440 |
|
| 441 |
const data = await apiResponse.json();
|