From a79c38c07ea74df808197bab0f2bf9129fa4b0bd Mon Sep 17 00:00:00 2001 From: cloudmu Date: Sat, 3 Sep 2016 05:32:19 -0400 Subject: [PATCH] Provide custom onError handler for http-proxy-middleware (#502) * Change http-proxy-middleware logLevel from silent to error * provide onError handler for httpProxyMiddleware --- scripts/start.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/scripts/start.js b/scripts/start.js index f63057b8be1..a0b01545b5b 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -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. @@ -209,6 +226,7 @@ function addMiddleware(devServer) { httpProxyMiddleware(pathname => mayProxy.test(pathname), { target: proxy, logLevel: 'silent', + onError: onProxyError(proxy), secure: false, changeOrigin: true })