diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d932707..07c87bc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [v3.0.3](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.3) + +- fix(pathFilter): handle errors + ## [v3.0.2](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.2) - refactor(dependency): replace is-plain-obj with is-plain-object ([#1031](https://github.com/chimurai/http-proxy-middleware/pull/1031)) diff --git a/src/http-proxy-middleware.ts b/src/http-proxy-middleware.ts index a7c2920b..54f054fb 100644 --- a/src/http-proxy-middleware.ts +++ b/src/http-proxy-middleware.ts @@ -1,7 +1,7 @@ import type * as net from 'net'; import type * as http from 'http'; import type * as https from 'https'; -import type { RequestHandler, Options, Filter } from './types'; +import type { RequestHandler, Options, Filter, Logger } from './types'; import * as httpProxy from 'http-proxy'; import { verifyConfig } from './configuration'; import { getPlugins } from './get-plugins'; @@ -10,6 +10,7 @@ import * as PathRewriter from './path-rewriter'; import * as Router from './router'; import { Debug as debug } from './debug'; import { getFunctionName } from './utils/function'; +import { getLogger } from './logger'; export class HttpProxyMiddleware { private wsInternalSubscribed = false; @@ -17,10 +18,12 @@ export class HttpProxyMiddleware { private proxyOptions: Options; private proxy: httpProxy; private pathRewriter; + private logger: Logger; constructor(options: Options) { verifyConfig(options); this.proxyOptions = options; + this.logger = getLogger(options as unknown as Options); debug(`create proxy server`); this.proxy = httpProxy.createProxyServer({}); @@ -109,7 +112,13 @@ export class HttpProxyMiddleware { pathFilter: Filter | undefined, req: http.IncomingMessage, ): boolean => { - return matchPathFilter(pathFilter, req.url, req); + try { + return matchPathFilter(pathFilter, req.url, req); + } catch (err) { + debug('Error: matchPathFilter() called with request url: ', `"${req.url}"`); + this.logger.error(err); + return false; + } }; /** diff --git a/test/e2e/http-proxy-middleware.spec.ts b/test/e2e/http-proxy-middleware.spec.ts index c08890a7..927cbf98 100644 --- a/test/e2e/http-proxy-middleware.spec.ts +++ b/test/e2e/http-proxy-middleware.spec.ts @@ -167,6 +167,34 @@ describe('E2E http-proxy-middleware', () => { const response = await agent.get(`/api/b/c/d`).expect(404); expect(response.status).toBe(404); }); + + it('should not proxy when filter throws Error', async () => { + const myError = new Error('MY_ERROR'); + const filter = (path, req) => { + throw myError; + }; + + const logger: Logger = { + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + }; + + agent = request( + createApp( + createProxyMiddleware({ + target: `http://localhost:${mockTargetServer.port}`, + pathFilter: filter, + logger: logger, + }), + ), + ); + + await mockTargetServer.forGet('/api/b/c/d').thenReply(200, 'HELLO WEB'); + const response = await agent.get(`/api/b/c/d`).expect(404); + expect(response.status).toBe(404); + expect(logger.error).toHaveBeenCalledWith(myError); + }); }); describe('multi path', () => {