diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ed4974a..5704e46d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [v1.1.2](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.2) + +- fix(log error): handle optional target ([#523](https://github.com/chimurai/http-proxy-middleware/pull/523)) + ## [v1.1.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.1) - fix(error handler): re-throw http-proxy missing target error ([#517](https://github.com/chimurai/http-proxy-middleware/pull/517)) diff --git a/README.md b/README.md index c5594bba..ad33d661 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li - **option.onError**: function, subscribe to http-proxy's `error` event for custom error handling. ```javascript - function onError(err, req, res) { + function onError(err, req, res, target) { res.writeHead(500, { 'Content-Type': 'text/plain', }); diff --git a/package.json b/package.json index 6f24e54d..4bfd0703 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "http-proxy-middleware", - "version": "1.1.1", + "version": "1.1.2", "description": "The one-liner node.js proxy middleware for connect, express and browser-sync", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/http-proxy-middleware.ts b/src/http-proxy-middleware.ts index 65792134..e7fec6e4 100644 --- a/src/http-proxy-middleware.ts +++ b/src/http-proxy-middleware.ts @@ -8,7 +8,6 @@ import * as handlers from './handlers'; import { getArrow, getInstance } from './logger'; import * as PathRewriter from './path-rewriter'; import * as Router from './router'; - export class HttpProxyMiddleware { private logger = getInstance(); private config: Config; @@ -184,13 +183,14 @@ export class HttpProxyMiddleware { } }; - private logError = (err, req: Request, res: Response) => { - const hostname = (req.headers && req.headers.host) || req.hostname || req.host; // (websocket) || (node0.10 || node 4/5) - const target = (this.proxyOptions.target as any).host || this.proxyOptions.target; - const errorMessage = - '[HPM] Error occurred while trying to proxy request %s from %s to %s (%s) (%s)'; + private logError = (err, req: Request, res: Response, target) => { + const hostname = req.headers?.host || req.hostname || req.host; // (websocket) || (node0.10 || node 4/5) + const requestHref = `${hostname}${req.url}`; + const targetHref = `${target.href}`; + + const errorMessage = '[HPM] Error occurred while proxying request %s to %s [%s] (%s)'; const errReference = 'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page - this.logger.error(errorMessage, req.url, hostname, target, err.code || err, errReference); + this.logger.error(errorMessage, requestHref, targetHref, err.code || err, errReference); }; }