From 3d7c6dfc29f57056286f347d2fd62af10847c51f Mon Sep 17 00:00:00 2001
From: FORESTIER Fabien <fabien.forestier@soprasteria.com>
Date: Fri, 17 Jan 2020 09:08:52 +0100
Subject: [PATCH] Catch errors on the different proxies and abort the proxy req
 if the incomming request has been cancelled by the client

---
 index.js | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/index.js b/index.js
index e62d65a..f742413 100644
--- a/index.js
+++ b/index.js
@@ -35,6 +35,43 @@ var mvtUnauthProxy = httpProxy.createProxyServer({
   target: proxyHostTarget,
 });
 
+// keep a referece of the proxyRequest in the req object
+wmsProxy.on('proxyReq', function (proxyReq, req) {
+  req._proxyReq = proxyReq;
+});
+
+mvtProxy.on('proxyReq', function (proxyReq, req) {
+  req._proxyReq = proxyReq;
+});
+
+mvtUnauthProxy.on('proxyReq', function (proxyReq, req) {
+  req._proxyReq = proxyReq;
+});
+
+wmsProxy.on('error', function (err, req, res) {
+  // If the client cancelled the request, abort the request to the upstream server
+  if (req.socket.destroyed && err.code === 'ECONNRESET') {
+    req._proxyReq.abort();
+  }
+  return console.log(`WMS Error, req.socket.destroyed: ${req.socket.destroyed}, ${err}`);
+});
+
+mvtProxy.on('error', function (err, req, res) {
+  // If the client cancelled the request, abort the request to the upstream server
+  if (req.socket.destroyed && err.code === 'ECONNRESET') {
+    req._proxyReq.abort();
+  }
+  return console.log(`MVT Error, req.socket.destroyed: ${req.socket.destroyed}, ${err}`);
+});
+
+mvtUnauthProxy.on('error', function (err, req, res) {
+  // If the client cancelled the request, abort the request to the upstream server
+  if (req.socket.destroyed && err.code === 'ECONNRESET') {
+    req._proxyReq.abort();
+  }
+  return console.log(`MVT Unauthenticated Error, req.socket.destroyed: ${req.socket.destroyed}, ${err}`);
+});
+
 // Create an HTTP server
 http.createServer(async function (req, res) {
 
-- 
GitLab