Skip to content

Commit

Permalink
fix(log error): handle optional target (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai authored Apr 14, 2021
1 parent 0381fdf commit 13bc071
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 7 additions & 7 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};
}

0 comments on commit 13bc071

Please sign in to comment.