Skip to content

Commit

Permalink
chore(error): logger socker error info
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Jun 20, 2018
1 parent 381223d commit c0148a0
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions bin/tsw/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,19 +556,43 @@ Ajax.prototype.doRequest = function(opt) {
request.once('socket', function(socket) {
if (socket.remoteAddress) {
this.remoteIp = socket.remoteAddress;
logger.debug(`${logPre}socket reuse ${socket.remoteAddress}:${socket.remotePort}`);
return;
}

const onError = (err) => {
logger.error(logPre + err.stack);
clean();
this.emit('fail');
};

const onConnect = function() {
logger.debug(`${logPre}connect ${this.remoteAddress}:${this.remotePort}`);
clean();
};

const onLookup = (err, address, family, host) => {
if (err) {
logger.error(logPre + err.stack);
clean();
this.emit('fail');
return;
}
this.remoteIp = address;
};

const clean = function() {
socket.removeListener('error', onError);
socket.removeListener('connect', onConnect);
socket.removeListener('lookup', onLookup);
};

if (!net.isIP(opt.host)) {
socket.once('lookup', (err, address, family, host) => {
if (err) {
logger.error(logPre + err.stack);
this.emit('fail');
return;
}
this.remoteIp = address;
});
socket.once('lookup', onLookup);
}

socket.once('connect', onConnect);
socket.once('error', onError);
});

defer.always(function() {
Expand Down

0 comments on commit c0148a0

Please sign in to comment.