chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.6k stars 828 forks source link

"Invalid context" error message is unhelpful when mixing paths with globs #964

Closed mcFrax closed 2 months ago

mcFrax commented 5 months ago

Checks

Describe the bug (be clear and concise)

This message:

ERR_CONTEXT_MATCHER_INVALID_ARRAY = '[HPM] Invalid pathFilter. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]',

doesn't explain that mixing globs and non-globs is prohibited. Although it shows one array with just non-glob paths and one with globs, it doesn't clarify that these are examples of 2 separate categories of pathFilters.

I had to find the answer by Googling the error message and reading the code that produces it.

Step-by-step reproduction instructions

createProxyMiddleware(['/api/**', '/foo'], {
    target: "https://httpbin.org",
    changeOrigin: true,
  })

https://codesandbox.io/p/devbox/http-proxy-middleware-forked-6ky4n8?file=%2Fsrc%2Findex.js%3A10%2C5

Error message:

[HPM] Invalid pathFilter. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]
<stack trace>

Expected behavior (be clear and concise)

Error explaining that globs and paths cannot be mixed. Perhaps just this explanation added on top of the existing message.

[HPM] Invalid pathFilter. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]. Plain paths (e.g. "/api") can't be mixed with globs (e.g. "/api/**").

How is http-proxy-middleware used in your project?

Indirectly, with Webpack's devServer.

What http-proxy-middleware configuration are you using?

[irrelevant]

What OS/version and node/version are you seeing the problem?

[irrelevant]

Additional context (optional)

The error is thrown in this line.

The only cases handled for array are pathFilter.every(isStringPath) and pathFilter.every(isGlobPath). isStringPath checks that the string is not a glob - so for mixed paths and globs neither of the conditions will be fullfilled.

chimurai commented 3 months ago

Hi @mcFrax

Thanks for suggestion. Sounds like a good improvement.

Care to create a PR with your suggestion to improve the error message?