Skip to content

Commit

Permalink
fix(filter): handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Oct 6, 2024
1 parent 1bd6dd5 commit 0b4274e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [v2.0.7](https://github.com/chimurai/http-proxy-middleware/releases/tag/v2.0.7)

- ci(github actions): add publish.yml
- fix(filter): handle errors

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-proxy-middleware",
"version": "2.0.7-beta.0",
"version": "2.0.7-beta.1",
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 7 additions & 2 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ export class HttpProxyMiddleware {
* @return {Boolean}
*/
private shouldProxy = (context, req: Request): boolean => {
const path = req.originalUrl || req.url;
return contextMatcher.match(context, path, req);
try {
const path = req.originalUrl || req.url;
return contextMatcher.match(context, path, req);
} catch (error) {
this.logger.error(error);
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 @@ -153,6 +153,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 = {
log: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

agent = request(
createApp(
createProxyMiddleware(filter, {
target: `http://localhost:${mockTargetServer.port}`,
logProvider: () => logger,
})
)
);

await mockTargetServer.get('/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(expect.objectContaining(myError));
});
});

describe('multi path', () => {
Expand Down

0 comments on commit 0b4274e

Please # to comment.