Skip to content

Commit 13bc071

Browse files
authored
fix(log error): handle optional target (#523)
1 parent 0381fdf commit 13bc071

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v1.1.2](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.2)
4+
5+
- fix(log error): handle optional target ([#523](https://github.com/chimurai/http-proxy-middleware/pull/523))
6+
37
## [v1.1.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.1)
48

59
- fix(error handler): re-throw http-proxy missing target error ([#517](https://github.com/chimurai/http-proxy-middleware/pull/517))

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
298298
- **option.onError**: function, subscribe to http-proxy's `error` event for custom error handling.
299299

300300
```javascript
301-
function onError(err, req, res) {
301+
function onError(err, req, res, target) {
302302
res.writeHead(500, {
303303
'Content-Type': 'text/plain',
304304
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-proxy-middleware",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/http-proxy-middleware.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as handlers from './handlers';
88
import { getArrow, getInstance } from './logger';
99
import * as PathRewriter from './path-rewriter';
1010
import * as Router from './router';
11-
1211
export class HttpProxyMiddleware {
1312
private logger = getInstance();
1413
private config: Config;
@@ -184,13 +183,14 @@ export class HttpProxyMiddleware {
184183
}
185184
};
186185

187-
private logError = (err, req: Request, res: Response) => {
188-
const hostname = (req.headers && req.headers.host) || req.hostname || req.host; // (websocket) || (node0.10 || node 4/5)
189-
const target = (this.proxyOptions.target as any).host || this.proxyOptions.target;
190-
const errorMessage =
191-
'[HPM] Error occurred while trying to proxy request %s from %s to %s (%s) (%s)';
186+
private logError = (err, req: Request, res: Response, target) => {
187+
const hostname = req.headers?.host || req.hostname || req.host; // (websocket) || (node0.10 || node 4/5)
188+
const requestHref = `${hostname}${req.url}`;
189+
const targetHref = `${target.href}`;
190+
191+
const errorMessage = '[HPM] Error occurred while proxying request %s to %s [%s] (%s)';
192192
const errReference = 'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page
193193

194-
this.logger.error(errorMessage, req.url, hostname, target, err.code || err, errReference);
194+
this.logger.error(errorMessage, requestHref, targetHref, err.code || err, errReference);
195195
};
196196
}

0 commit comments

Comments
 (0)