Skip to content

Commit

Permalink
fix: forward error requests to the proxy (#2425)
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverJAsh authored Feb 5, 2020
1 parent 7ee8bc0 commit e291cd4
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 e291cd4

Please sign in to comment.