Commit ec60307c authored by cloudmu's avatar cloudmu Committed by Dan Abramov
Browse files

Provide custom onError handler for http-proxy-middleware (#502)

* Change http-proxy-middleware logLevel from silent to error

* provide onError handler for httpProxyMiddleware
parent ae36f2df
No related merge requests found
Showing with 18 additions and 0 deletions
+18 -0
...@@ -170,6 +170,23 @@ function openBrowser(port, protocol) { ...@@ -170,6 +170,23 @@ function openBrowser(port, protocol) {
opn(protocol + '://localhost:' + port + '/'); opn(protocol + '://localhost:' + port + '/');
} }
// We need to provide a custom onError function for httpProxyMiddleware.
// It allows us to log custom error messages on the console.
function onProxyError(proxy) {
return function(err, req, res){
var host = req.headers && req.headers.host;
console.log(
chalk.red('Proxy error:') + ' Could not proxy request ' + chalk.cyan(req.url) +
' from ' + chalk.cyan(host) + ' to ' + chalk.cyan(proxy) + '.'
);
console.log(
'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' +
chalk.cyan(err.code) + ').'
);
console.log();
}
}
function addMiddleware(devServer) { function addMiddleware(devServer) {
// `proxy` lets you to specify a fallback server during development. // `proxy` lets you to specify a fallback server during development.
// Every unrecognized request will be forwarded to it. // Every unrecognized request will be forwarded to it.
...@@ -209,6 +226,7 @@ function addMiddleware(devServer) { ...@@ -209,6 +226,7 @@ function addMiddleware(devServer) {
httpProxyMiddleware(pathname => mayProxy.test(pathname), { httpProxyMiddleware(pathname => mayProxy.test(pathname), {
target: proxy, target: proxy,
logLevel: 'silent', logLevel: 'silent',
onError: onProxyError(proxy),
secure: false, secure: false,
changeOrigin: true changeOrigin: true
}) })
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment