Skip to content

Commit

Permalink
Provide custom onError handler for http-proxy-middleware (facebook#502)
Browse files Browse the repository at this point in the history
* Change http-proxy-middleware logLevel from silent to error

* provide onError handler for httpProxyMiddleware
  • Loading branch information
cloudmu authored and feiqitian committed Oct 25, 2016
1 parent e0f4e61 commit a79c38c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ function openBrowser(port, protocol) {
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) {
// `proxy` lets you to specify a fallback server during development.
// Every unrecognized request will be forwarded to it.
Expand Down Expand Up @@ -209,6 +226,7 @@ function addMiddleware(devServer) {
httpProxyMiddleware(pathname => mayProxy.test(pathname), {
target: proxy,
logLevel: 'silent',
onError: onProxyError(proxy),
secure: false,
changeOrigin: true
})
Expand Down

0 comments on commit a79c38c

Please sign in to comment.