From 3c11956ca5b9ab0d2757295d196e49691c63be5c Mon Sep 17 00:00:00 2001 From: Jacek Date: Tue, 11 Mar 2025 20:09:15 -0500 Subject: [PATCH] fix(repo): Handle errors in the proxy server --- integration/scripts/proxyServer.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/integration/scripts/proxyServer.ts b/integration/scripts/proxyServer.ts index 1a9011f3fdb..5a16302b039 100644 --- a/integration/scripts/proxyServer.ts +++ b/integration/scripts/proxyServer.ts @@ -1,3 +1,4 @@ +import type { IncomingMessage, ServerResponse } from 'node:http'; import http from 'node:http'; import type { createServer as _createServer, Server, ServerOptions } from 'node:https'; import https from 'node:https'; @@ -14,14 +15,23 @@ type ProxyServerOptions = { * The server will listen on port 80 (http) or 443 (https) depending on whether SSL options are provided. */ export const createProxyServer = (opts: ProxyServerOptions) => { - const proxy = httpProxy.createProxyServer({ xfwd: true }); const usingSSL = !!opts.ssl; + + const proxy = httpProxy.createProxyServer({ + secure: usingSSL, + xfwd: true, + }); + + // We need to handle errors to avoid crashing the proxy server + proxy.on('error', (err: Error, req: IncomingMessage, res: ServerResponse) => { + console.error(`[Proxy Error]: ${req.url}`, err); + res.writeHead(502); + res.end('Proxy error'); + }); + const createServer: typeof _createServer = usingSSL ? https.createServer.bind(https) : http.createServer.bind(http); return createServer(opts.ssl, (req, res) => { - console.log(`/n/n/n/n------------------------------------`); - console.log('Proxying request', req.headers.host, req.url); - console.log('Headers', req.headers); const hostHeader = req.headers.host || ''; if (opts.targets[hostHeader]) { proxy.web(req, res, { target: opts.targets[hostHeader] });