Skip to content

Commit

Permalink
Also forward error requests to the proxy
Browse files Browse the repository at this point in the history
Fixes #2380
  • Loading branch information
OliverJAsh committed Feb 3, 2020
1 parent 638103f commit 29a822e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/Server.js
Expand Up @@ -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();

Expand Down Expand Up @@ -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));
});
}

Expand Down
17 changes: 17 additions & 0 deletions test/server/proxy-option.test.js
Expand Up @@ -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');
});
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 29a822e

Please sign in to comment.