Skip to content

Commit 788b21e

Browse files
authored
fix(pathFilter): handle errors (#1043)
1 parent 6fae40d commit 788b21e

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v3.0.3](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.3)
4+
5+
- fix(pathFilter): handle errors
6+
37
## [v3.0.2](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.2)
48

59
- refactor(dependency): replace is-plain-obj with is-plain-object ([#1031](https://github.com/chimurai/http-proxy-middleware/pull/1031))

src/http-proxy-middleware.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as net from 'net';
22
import type * as http from 'http';
33
import type * as https from 'https';
4-
import type { RequestHandler, Options, Filter } from './types';
4+
import type { RequestHandler, Options, Filter, Logger } from './types';
55
import * as httpProxy from 'http-proxy';
66
import { verifyConfig } from './configuration';
77
import { getPlugins } from './get-plugins';
@@ -10,17 +10,20 @@ import * as PathRewriter from './path-rewriter';
1010
import * as Router from './router';
1111
import { Debug as debug } from './debug';
1212
import { getFunctionName } from './utils/function';
13+
import { getLogger } from './logger';
1314

1415
export class HttpProxyMiddleware<TReq, TRes> {
1516
private wsInternalSubscribed = false;
1617
private serverOnCloseSubscribed = false;
1718
private proxyOptions: Options<TReq, TRes>;
1819
private proxy: httpProxy<TReq, TRes>;
1920
private pathRewriter;
21+
private logger: Logger;
2022

2123
constructor(options: Options<TReq, TRes>) {
2224
verifyConfig<TReq, TRes>(options);
2325
this.proxyOptions = options;
26+
this.logger = getLogger(options as unknown as Options);
2427

2528
debug(`create proxy server`);
2629
this.proxy = httpProxy.createProxyServer({});
@@ -109,7 +112,13 @@ export class HttpProxyMiddleware<TReq, TRes> {
109112
pathFilter: Filter<TReq> | undefined,
110113
req: http.IncomingMessage,
111114
): boolean => {
112-
return matchPathFilter(pathFilter, req.url, req);
115+
try {
116+
return matchPathFilter(pathFilter, req.url, req);
117+
} catch (err) {
118+
debug('Error: matchPathFilter() called with request url: ', `"${req.url}"`);
119+
this.logger.error(err);
120+
return false;
121+
}
113122
};
114123

115124
/**

test/e2e/http-proxy-middleware.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,34 @@ describe('E2E http-proxy-middleware', () => {
167167
const response = await agent.get(`/api/b/c/d`).expect(404);
168168
expect(response.status).toBe(404);
169169
});
170+
171+
it('should not proxy when filter throws Error', async () => {
172+
const myError = new Error('MY_ERROR');
173+
const filter = (path, req) => {
174+
throw myError;
175+
};
176+
177+
const logger: Logger = {
178+
info: jest.fn(),
179+
warn: jest.fn(),
180+
error: jest.fn(),
181+
};
182+
183+
agent = request(
184+
createApp(
185+
createProxyMiddleware({
186+
target: `http://localhost:${mockTargetServer.port}`,
187+
pathFilter: filter,
188+
logger: logger,
189+
}),
190+
),
191+
);
192+
193+
await mockTargetServer.forGet('/api/b/c/d').thenReply(200, 'HELLO WEB');
194+
const response = await agent.get(`/api/b/c/d`).expect(404);
195+
expect(response.status).toBe(404);
196+
expect(logger.error).toHaveBeenCalledWith(myError);
197+
});
170198
});
171199

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

0 commit comments

Comments
 (0)