diff --git a/packages/microfrontends/src/bin/local-proxy.ts b/packages/microfrontends/src/bin/local-proxy.ts index 39ed036..a50516d 100644 --- a/packages/microfrontends/src/bin/local-proxy.ts +++ b/packages/microfrontends/src/bin/local-proxy.ts @@ -477,6 +477,15 @@ export class LocalProxy { this.proxy = Server.createProxyServer({ secure: true }); this.proxy.on('error', (err, req, res) => { if (res instanceof http.ServerResponse) { + /** Already closed; nothing left to send. */ + if (res.destroyed || res.writableEnded) return; + + /** Headers are committed; abort instead of writing a 500. */ + if (res.headersSent) { + res.destroy(err); + return; + } + res.writeHead(500, { 'Content-Type': 'text/plain', }); @@ -701,6 +710,16 @@ export class LocalProxy { req.pipe(proxyReq); proxyReq.on('error', (err) => { logger.error('Proxy request error: ', err); + + /** Already closed; nothing left to send. */ + if (res.destroyed || res.writableEnded) return; + + /** Headers are committed; abort instead of writing a 500. */ + if (res.headersSent) { + res.destroy(err); + return; + } + res.writeHead(500, { 'Content-Type': 'text/plain' }); res.end( `Error proxying request for ${target.application} to ${hostname}:${port}${path}`,