-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some errors are not handled correctly for proxied requests #2380
Comments
We don't do it, can you create minimum reproducible test repo? |
Thanks, i will look at this in near future |
WIP |
It is problem from app.use((error, req, res, next) => {
var err = null;
try {
decodeURIComponent(req.path)
}
catch(e) {
err = e;
}
if (err){
res.send("my custom error page");
}
next();
}); But we can't do it because we use a lot of middlewares which doesn't handle that error, sorry Feel free to feedback, anyway what is use case, it is broken URL |
I want my development environment to behave the same as production. I'm working on the error page for invalid URLs but because of this bug I cannot see that error page in development (i.e. when using this module). |
You can use the |
I have already done this on my own server. The issue is that the proxy used by |
Express correctly parse your request, you have invalid URL and it is expected behavior, if you need change that behavior please use the |
If you make a request to the server (skipping the proxy), you will see the error page. ✅ $ curl "localhost:3000/%"
my custom error page If you make a request to the proxy, you will not see the error page. ❌ $ curl "localhost:4000/api/%"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>URIError: Failed to decode param '/%'<br> at decodeURIComponent (<anonymous>)<br> at decode_param (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/layer.js:172:12)<br> at Layer.match (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/layer.js:123:27)<br> at matchLayer (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:574:18)<br> at next (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:220:15)<br> at expressInit (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/middleware/init.js:40:5)<br> at Layer.handle [as handle_request] (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/layer.js:95:5)<br> at trim_prefix (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:317:13)<br> at /Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:284:7<br> at Function.process_params (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:335:12)</pre>
</body>
</html> Do you see what's wrong? The proxy does not preserve behaviour of the server. |
Do you see error stack? It is from express because it is default behavior |
I tried to workaround this bug by using the module.exports = {
devServer: {
before: (app, server, compiler, proxy) => {
// Forward error requests to the proxy so it can handle them.
app.use((error, req, res, next) => proxy(req, res, next));
},
}
}; … but unfortunately this is not possible—the In any case, I still believe this is something that should be handled by Consider a simple server and proxy: const httpProxyMiddleware = require("http-proxy-middleware");
const express = require("express");
{
const app = express();
app.get("*", (req, res) => {
res.send("my home page");
});
app.use((error, req, res, next) => {
res.send("my custom error page");
});
app.listen(3000);
}
{
const app = express();
app.all("*", (req, res, next) => {
next();
});
const proxy = httpProxyMiddleware({ target: "http://localhost:3000" });
app.use(proxy);
app.listen(4000);
} If you make a request to the server (skipping the proxy), you will see the error page. ✅ $ curl "localhost:3000/%"
my custom error page If you make a request to the proxy, you will not see the error page. ❌ $ curl "localhost:4000/%"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>URIError: Failed to decode param '/%'<br> at decodeURIComponent (<anonymous>)<br> at decode_param (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/layer.js:172:12)<br> at Layer.match (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/layer.js:123:27)<br> at matchLayer (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:574:18)<br> at next (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:220:15)<br> at expressInit (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/middleware/init.js:40:5)<br> at Layer.handle [as handle_request] (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/layer.js:95:5)<br> at trim_prefix (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:317:13)<br> at /Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:284:7<br> at Function.process_params (/Users/oliverash/Development/webpack-dev-server-uri-error-test/node_modules/express/lib/router/index.js:335:12)</pre>
</body>
</html> As you can see, errors are being handled by the proxy server rather than the server itself. We would rather just forward these requests so they can be handled by the original server. All we have to do is this: const proxy = httpProxyMiddleware({ target: "http://localhost:3000" });
+ // Forward error requests to the proxy so it can handle them.
+ app.use((error, req, res, next) => proxy(req, res, next));
app.use(proxy); Now, if you make a request to the proxy, you will see the error page. ✅ $ curl "localhost:4000/%"
my custom error page This change would be very easy to incorporate into Potentially related: chimurai/http-proxy-middleware#311 |
Okay, send a PR, don't forget add tests |
package.json
belowpackage.json
belowCode
Expected Behavior
node index.js
webpack-dev-server
curl "localhost:4000/api/%"
The response should be
"my custom error page"
, indicating that the Express error handling middleware for the API server was used.Actual Behavior
The response is
URIError: Failed to decode param …
, indicating that the Express error handling middleware for the API server was not used.I would not expect
webpack-dev-server
to change how errors are handled for proxied requests.Technical notes
I played around with using
http-proxy-middleware
on its own to see if the root issue was inside there, but using the following code, I could not reproduce this issue:For Bugs; How can we reproduce the behavior?
See above
For Features; What is the motivation and/or use-case for the feature?
N/A
The text was updated successfully, but these errors were encountered: