Skip to content

Commit

Permalink
fix(pathFilter): handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Oct 6, 2024
1 parent 6fae40d commit 1b14e13
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
13 changes: 11 additions & 2 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,17 +10,20 @@ 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<TReq, TRes> {
private wsInternalSubscribed = false;
private serverOnCloseSubscribed = false;
private proxyOptions: Options<TReq, TRes>;
private proxy: httpProxy<TReq, TRes>;
private pathRewriter;
private logger: Logger;

constructor(options: Options<TReq, TRes>) {
verifyConfig<TReq, TRes>(options);
this.proxyOptions = options;
this.logger = getLogger(options as unknown as Options);

debug(`create proxy server`);
this.proxy = httpProxy.createProxyServer({});
Expand Down Expand Up @@ -109,7 +112,13 @@ export class HttpProxyMiddleware<TReq, TRes> {
pathFilter: Filter<TReq> | 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;
}
};

/**
Expand Down
28 changes: 28 additions & 0 deletions test/e2e/http-proxy-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 1b14e13

Please sign in to comment.