Skip to content

Commit 0cb62af

Browse files
authored
fix(proxy): surface upstream error cause and distinguish timeouts (#757)
1 parent ceb4ba0 commit 0cb62af

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

packages/script/src/runtime/server/proxy-handler.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,11 @@ export default defineEventHandler(async (event) => {
355355
log('[proxy] Fetching:', targetUrl)
356356

357357
const controller = new AbortController()
358-
const timeoutId = setTimeout(() => controller.abort(), 15000) // 15s timeout
358+
let timedOut = false
359+
const timeoutId = setTimeout(() => {
360+
timedOut = true
361+
controller.abort()
362+
}, 15000) // 15s timeout
359363

360364
// Resolve the fetch body: passthrough streams the raw request, otherwise serialize
361365
let fetchBody: BodyInit | undefined
@@ -381,9 +385,14 @@ export default defineEventHandler(async (event) => {
381385
catch (err) {
382386
log('[proxy] Upstream error:', err)
383387
throw createError({
384-
statusCode: 502,
385-
statusMessage: 'Bad Gateway',
388+
statusCode: timedOut ? 504 : 502,
389+
statusMessage: timedOut ? 'Gateway Timeout' : 'Bad Gateway',
386390
message: `Proxy upstream request failed: ${targetUrl}`,
391+
cause: err,
392+
data: {
393+
errorName: (err as Error)?.name,
394+
errorCode: timedOut ? 'TIMEOUT' : (err as { code?: string })?.code,
395+
},
387396
})
388397
}
389398
finally {

0 commit comments

Comments
 (0)