Skip to content

Commit 1dbd08c

Browse files
authoredMar 12, 2025
fix(repo): Handle errors in E2E proxy server (#5328)
1 parent ba2b00c commit 1dbd08c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed
 

‎integration/scripts/proxyServer.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { IncomingMessage, ServerResponse } from 'node:http';
12
import http from 'node:http';
23
import type { createServer as _createServer, Server, ServerOptions } from 'node:https';
34
import https from 'node:https';
@@ -14,14 +15,23 @@ type ProxyServerOptions = {
1415
* The server will listen on port 80 (http) or 443 (https) depending on whether SSL options are provided.
1516
*/
1617
export const createProxyServer = (opts: ProxyServerOptions) => {
17-
const proxy = httpProxy.createProxyServer({ xfwd: true });
1818
const usingSSL = !!opts.ssl;
19+
20+
const proxy = httpProxy.createProxyServer({
21+
secure: usingSSL,
22+
xfwd: true,
23+
});
24+
25+
// We need to handle errors to avoid crashing the proxy server
26+
proxy.on('error', (err: Error, req: IncomingMessage, res: ServerResponse) => {
27+
console.error(`[Proxy Error]: ${req.url}`, err);
28+
res.writeHead(502);
29+
res.end('Proxy error');
30+
});
31+
1932
const createServer: typeof _createServer = usingSSL ? https.createServer.bind(https) : http.createServer.bind(http);
2033

2134
return createServer(opts.ssl, (req, res) => {
22-
console.log(`/n/n/n/n------------------------------------`);
23-
console.log('Proxying request', req.headers.host, req.url);
24-
console.log('Headers', req.headers);
2535
const hostHeader = req.headers.host || '';
2636
if (opts.targets[hostHeader]) {
2737
proxy.web(req, res, { target: opts.targets[hostHeader] });

0 commit comments

Comments
 (0)