Skip to content

Commit

Permalink
feat(logging): add error description when logging errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudmu committed Aug 30, 2016
1 parent 28030c5 commit 9614dd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var _ = require('lodash');
var httpProxy = require('http-proxy');
var errno = require('errno');
var configFactory = require('./config-factory');
var handlers = require('./handlers');
var contextMatcher = require('./context-matcher');
Expand Down Expand Up @@ -128,9 +129,14 @@ function HttpProxyMiddleware(context, opts) {
}

function logError(err, req, res) {
var hostname = (req.hostname || req.host) || (req.headers && req.headers.host); // (node0.10 || node 4/5) || (websocket)
var targetUri = (proxyOptions.target.host || proxyOptions.target) + req.url;

logger.error('[HPM] PROXY ERROR: %s. %s -> %s', err.code, hostname, targetUri);
var hostname = (req.headers && req.headers.host) || (req.hostname || req.host); // (websocket) || (node0.10 || node 4/5)
var target = proxyOptions.target.host || proxyOptions.target;
var description = errno.code[err.code] && errno.code[err.code].description; // description for the error code

if (description) {
logger.error('[HPM] Error \'%s\' (ErrorCode=%s) occurred while trying to proxy request %s from %s to %s', description, err.code, req.url, hostname, target);
} else {
logger.error('[HPM] Error (ErrorCode=%s) occurred while trying to proxy request %s from %s to %s', err.code, req.url, hostname, target);
}
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"http-proxy": "^1.14.0",
"is-glob": "^2.0.1",
"lodash": "^4.14.2",
"micromatch": "^2.3.11"
"micromatch": "^2.3.11",
"errno": "^0.1.4"
}
}

0 comments on commit 9614dd0

Please sign in to comment.