diff --git a/packages/vite/src/node/server/middlewares/proxy.ts b/packages/vite/src/node/server/middlewares/proxy.ts index 7c6d2b26fbd4b0..aade3270ff29fc 100644 --- a/packages/vite/src/node/server/middlewares/proxy.ts +++ b/packages/vite/src/node/server/middlewares/proxy.ts @@ -106,7 +106,7 @@ export function proxyMiddleware( ) if (!res.headersSent && !res.writableEnded) { res - .writeHead(500, { + .writeHead(502, { 'Content-Type': 'text/plain', }) .end() diff --git a/playground/proxy-bypass/__tests__/proxy-bypass.spec.ts b/playground/proxy-bypass/__tests__/proxy-bypass.spec.ts index b133f543f2e813..c7b26adfdba0c4 100644 --- a/playground/proxy-bypass/__tests__/proxy-bypass.spec.ts +++ b/playground/proxy-bypass/__tests__/proxy-bypass.spec.ts @@ -1,5 +1,5 @@ import { expect, test, vi } from 'vitest' -import { browserLogs, page, serverLogs } from '~utils' +import { browserLogs, isServe, page, serverLogs, viteTestUrl } from '~utils' test('proxy-bypass', async () => { await vi.waitFor(() => { @@ -17,3 +17,8 @@ test('async-proxy-bypass-with-error', async () => { expect(serverLogs.join('\n')).toContain('bypass error') }) }) + +test.runIf(isServe)('proxy error returns 502', async () => { + const res = await fetch(viteTestUrl + '/proxyError') + expect(res.status).toBe(502) +}) diff --git a/playground/proxy-bypass/vite.config.js b/playground/proxy-bypass/vite.config.js index 1b40aebe5b2968..2b190df0ff8250 100644 --- a/playground/proxy-bypass/vite.config.js +++ b/playground/proxy-bypass/vite.config.js @@ -12,6 +12,9 @@ export default defineConfig({ return false }, }, + '/proxyError': { + target: 'http://foo.invalid', + }, '/asyncResponse': { bypass: async (_, res) => { await timeout(4)