Skip to content

Commit e291cd4

Browse files
authoredFeb 5, 2020
fix: forward error requests to the proxy (#2425)
1 parent 7ee8bc0 commit e291cd4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

‎lib/Server.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class Server {
288288
this.websocketProxies.push(proxyMiddleware);
289289
}
290290

291-
this.app.use((req, res, next) => {
291+
const handle = (req, res, next) => {
292292
if (typeof proxyConfigOrCallback === 'function') {
293293
const newProxyConfig = proxyConfigOrCallback();
294294

@@ -319,7 +319,11 @@ class Server {
319319
} else {
320320
next();
321321
}
322-
});
322+
};
323+
324+
this.app.use(handle);
325+
// Also forward error requests to the proxy so it can handle them.
326+
this.app.use((error, req, res, next) => handle(req, res, next));
323327
});
324328
}
325329

‎test/server/proxy-option.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,19 @@ describe('proxy option', () => {
326326
// Parse application/json
327327
proxy.use(bodyParser.json());
328328

329+
// This forces Express to try to decode URLs, which is needed for the test
330+
// associated with the middleware below.
331+
proxy.all('*', (_req, res, next) => {
332+
next();
333+
});
334+
// We must define all 4 params in order for this to be detected as an
335+
// error handling middleware.
336+
// eslint-disable-next-line no-unused-vars
337+
proxy.use((error, proxyReq, res, next) => {
338+
res.status(500);
339+
res.send('error from proxy');
340+
});
341+
329342
proxy.get('/get', (proxyReq, res) => {
330343
res.send('GET method from proxy');
331344
});
@@ -372,6 +385,10 @@ describe('proxy option', () => {
372385
});
373386
});
374387

388+
it('errors', (done) => {
389+
req.get('/%').expect(500, 'error from proxy', done);
390+
});
391+
375392
it('GET method', (done) => {
376393
req.get('/get').expect(200, 'GET method from proxy', done);
377394
});

0 commit comments

Comments
 (0)
Please sign in to comment.