feat: return non-OK responses from fetchWithRetry and pass upstream errors through the proxy API.

This commit is contained in:
kuekhaoyang
2025-11-29 16:55:30 +08:00
parent 484dad5cdc
commit 39523e373a
2 changed files with 23 additions and 2 deletions
+13
View File
@@ -26,6 +26,19 @@ export async function GET(request: NextRequest) {
const response = await fetchWithRetry({ url, request, headers: requestHeaders });
// If upstream returned an error, pass it through with CORS headers
if (!response.ok) {
const errorText = await response.text();
return new NextResponse(errorText || `Upstream error: ${response.status}`, {
status: response.status,
statusText: response.statusText,
headers: {
'Content-Type': response.headers.get('Content-Type') || 'text/plain',
'Access-Control-Allow-Origin': '*',
},
});
}
const contentType = response.headers.get('Content-Type');
// Better M3U8 detection: check both content-type and actual content
+10 -2
View File
@@ -76,8 +76,16 @@ export async function fetchWithRetry({ url, request, headers = {} }: FetchWithRe
}
}
if (!response || !response.ok) {
throw new Error(`Failed after ${MAX_RETRIES} attempts: ${response?.status || lastError}`);
// If we got a response (even an error response like 403, 404), return it
// Only throw if we truly failed to get any response
if (!response) {
throw new Error(`Failed after ${MAX_RETRIES} attempts: ${lastError}`);
}
// Return the response even if it's an error status (403, 404, etc.)
// The caller can check response.ok or response.status
if (!response.ok) {
console.warn(`⚠ Returning non-OK response: ${response.status} ${response.statusText}`);
}
return response;