Skip to content

Commit e240df2

Browse files
Copilotsapphi-red
andauthored
fix(dev): return "502 Bad Gateway" on proxy failures instead of 500 (#21652)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent 44b5bdf commit e240df2

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

packages/vite/src/node/server/middlewares/proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function proxyMiddleware(
106106
)
107107
if (!res.headersSent && !res.writableEnded) {
108108
res
109-
.writeHead(500, {
109+
.writeHead(502, {
110110
'Content-Type': 'text/plain',
111111
})
112112
.end()

playground/proxy-bypass/__tests__/proxy-bypass.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test, vi } from 'vitest'
2-
import { browserLogs, page, serverLogs } from '~utils'
2+
import { browserLogs, isServe, page, serverLogs, viteTestUrl } from '~utils'
33

44
test('proxy-bypass', async () => {
55
await vi.waitFor(() => {
@@ -17,3 +17,8 @@ test('async-proxy-bypass-with-error', async () => {
1717
expect(serverLogs.join('\n')).toContain('bypass error')
1818
})
1919
})
20+
21+
test.runIf(isServe)('proxy error returns 502', async () => {
22+
const res = await fetch(viteTestUrl + '/proxyError')
23+
expect(res.status).toBe(502)
24+
})

playground/proxy-bypass/vite.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export default defineConfig({
1212
return false
1313
},
1414
},
15+
'/proxyError': {
16+
target: 'http://foo.invalid',
17+
},
1518
'/asyncResponse': {
1619
bypass: async (_, res) => {
1720
await timeout(4)

0 commit comments

Comments
 (0)