From 29a822ea8426820617745b3f355c344e84f7c01b Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Mon, 3 Feb 2020 12:35:50 +0000 Subject: [PATCH] Also forward error requests to the proxy Fixes https://github.com/webpack/webpack-dev-server/issues/2380 --- lib/Server.js | 8 ++++++-- test/server/proxy-option.test.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 1e8cf2d1b4..166c02f6c6 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -288,7 +288,7 @@ class Server { this.websocketProxies.push(proxyMiddleware); } - this.app.use((req, res, next) => { + const handle = (req, res, next) => { if (typeof proxyConfigOrCallback === 'function') { const newProxyConfig = proxyConfigOrCallback(); @@ -319,7 +319,11 @@ class Server { } else { next(); } - }); + }; + + this.app.use(handle); + // Also forward error requests to the proxy so it can handle them. + this.app.use((error, req, res, next) => handle(req, res, next)); }); } diff --git a/test/server/proxy-option.test.js b/test/server/proxy-option.test.js index 49368d3627..000c5199ad 100644 --- a/test/server/proxy-option.test.js +++ b/test/server/proxy-option.test.js @@ -326,6 +326,19 @@ describe('proxy option', () => { // Parse application/json proxy.use(bodyParser.json()); + // This forces Express to try to decode URLs, which is needed for the test + // associated with the middleware below. + proxy.all('*', (_req, res, next) => { + next(); + }); + // We must define all 4 params in order for this to be detected as an + // error handling middleware. + // eslint-disable-next-line no-unused-vars + proxy.use((error, proxyReq, res, next) => { + res.status(500); + res.send('error from proxy'); + }); + proxy.get('/get', (proxyReq, res) => { res.send('GET method from proxy'); }); @@ -372,6 +385,10 @@ describe('proxy option', () => { }); }); + it('errors', (done) => { + req.get('/%').expect(500, 'error from proxy', done); + }); + it('GET method', (done) => { req.get('/get').expect(200, 'GET method from proxy', done); });