Skip to content

Commit 0b4274e

Browse files
committed
fix(filter): handle errors
1 parent 1bd6dd5 commit 0b4274e

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [v2.0.7](https://github.com/chimurai/http-proxy-middleware/releases/tag/v2.0.7)
44

55
- ci(github actions): add publish.yml
6+
- fix(filter): handle errors
67

78
## [v2.0.6](https://github.com/chimurai/http-proxy-middleware/releases/tag/v2.0.6)
89

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-proxy-middleware",
3-
"version": "2.0.7-beta.0",
3+
"version": "2.0.7-beta.1",
44
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

Diff for: src/http-proxy-middleware.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ export class HttpProxyMiddleware {
109109
* @return {Boolean}
110110
*/
111111
private shouldProxy = (context, req: Request): boolean => {
112-
const path = req.originalUrl || req.url;
113-
return contextMatcher.match(context, path, req);
112+
try {
113+
const path = req.originalUrl || req.url;
114+
return contextMatcher.match(context, path, req);
115+
} catch (error) {
116+
this.logger.error(error);
117+
return false;
118+
}
114119
};
115120

116121
/**

Diff for: test/e2e/http-proxy-middleware.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,34 @@ describe('E2E http-proxy-middleware', () => {
153153
const response = await agent.get(`/api/b/c/d`).expect(404);
154154
expect(response.status).toBe(404);
155155
});
156+
157+
it('should not proxy when filter throws Error', async () => {
158+
const myError = new Error('MY_ERROR');
159+
const filter = (path, req) => {
160+
throw myError;
161+
};
162+
163+
const logger = {
164+
log: jest.fn(),
165+
info: jest.fn(),
166+
warn: jest.fn(),
167+
error: jest.fn(),
168+
};
169+
170+
agent = request(
171+
createApp(
172+
createProxyMiddleware(filter, {
173+
target: `http://localhost:${mockTargetServer.port}`,
174+
logProvider: () => logger,
175+
})
176+
)
177+
);
178+
179+
await mockTargetServer.get('/api/b/c/d').thenReply(200, 'HELLO WEB');
180+
const response = await agent.get(`/api/b/c/d`).expect(404);
181+
expect(response.status).toBe(404);
182+
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining(myError));
183+
});
156184
});
157185

158186
describe('multi path', () => {

0 commit comments

Comments
 (0)