From 4848e413a1db81957e2e4a263d1bd0c5a733ac56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=B1=84=EC=A4=80=20-=20CJ=20Lee?= Date: Mon, 24 Jul 2023 17:01:14 +0900 Subject: [PATCH] fix(proxy): handle error when proxy itself errors (#13929) --- packages/vite/src/node/server/middlewares/proxy.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/proxy.ts b/packages/vite/src/node/server/middlewares/proxy.ts index 6b282e0afea05b..bf658a9ea7474f 100644 --- a/packages/vite/src/node/server/middlewares/proxy.ts +++ b/packages/vite/src/node/server/middlewares/proxy.ts @@ -52,8 +52,17 @@ export function proxyMiddleware( proxy.on('error', (err, req, originalRes) => { // When it is ws proxy, res is net.Socket - const res = originalRes as http.ServerResponse | net.Socket - if ('req' in res) { + // originalRes can be falsy if the proxy itself errored + const res = originalRes as http.ServerResponse | net.Socket | undefined + if (!res) { + config.logger.error( + `${colors.red(`http proxy error: ${err.message}`)}\n${err.stack}`, + { + timestamp: true, + error: err, + }, + ) + } else if ('req' in res) { config.logger.error( `${colors.red(`http proxy error at ${originalRes.req.url}:`)}\n${ err.stack