Skip to content

Commit 39523e3

Browse files
committed
feat: return non-OK responses from fetchWithRetry and pass upstream errors through the proxy API.
1 parent 484dad5 commit 39523e3

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

app/api/proxy/route.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ export async function GET(request: NextRequest) {
2626

2727
const response = await fetchWithRetry({ url, request, headers: requestHeaders });
2828

29+
// If upstream returned an error, pass it through with CORS headers
30+
if (!response.ok) {
31+
const errorText = await response.text();
32+
return new NextResponse(errorText || `Upstream error: ${response.status}`, {
33+
status: response.status,
34+
statusText: response.statusText,
35+
headers: {
36+
'Content-Type': response.headers.get('Content-Type') || 'text/plain',
37+
'Access-Control-Allow-Origin': '*',
38+
},
39+
});
40+
}
41+
2942
const contentType = response.headers.get('Content-Type');
3043

3144
// Better M3U8 detection: check both content-type and actual content

lib/utils/fetch-with-retry.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,16 @@ export async function fetchWithRetry({ url, request, headers = {} }: FetchWithRe
7676
}
7777
}
7878

79-
if (!response || !response.ok) {
80-
throw new Error(`Failed after ${MAX_RETRIES} attempts: ${response?.status || lastError}`);
79+
// If we got a response (even an error response like 403, 404), return it
80+
// Only throw if we truly failed to get any response
81+
if (!response) {
82+
throw new Error(`Failed after ${MAX_RETRIES} attempts: ${lastError}`);
83+
}
84+
85+
// Return the response even if it's an error status (403, 404, etc.)
86+
// The caller can check response.ok or response.status
87+
if (!response.ok) {
88+
console.warn(`⚠ Returning non-OK response: ${response.status} ${response.statusText}`);
8189
}
8290

8391
return response;

0 commit comments

Comments
 (0)