Skip to content

Commit

Permalink
exposed errors for checkPortStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
EndangeredMassa committed Oct 11, 2013
1 parent 3817691 commit b8acb18
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ portscanner.checkPortStatus = function(port, host, callback) {
host = host || 'localhost'
var socket = new Socket()
, status = null
, error = null

// Socket connection established, port is open
socket.on('connect', function() {
Expand All @@ -64,21 +65,25 @@ portscanner.checkPortStatus = function(port, host, callback) {
})

// If no response, assume port is not listening
socket.setTimeout(400)
timeout = 400
socket.setTimeout(timeout)
socket.on('timeout', function() {
status = 'closed'
error = new Error('Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available')
socket.destroy()
})

// Assuming the port is not open if an error. May need to refine based on
// exception
socket.on('error', function(exception) {
error = exception
status = 'closed'
})

// Return after the socket has closed
socket.on('close', function(exception) {
callback(null, status)
if (exception) error = exception
callback(error, status)
})

socket.connect(port, host)
Expand Down

0 comments on commit b8acb18

Please sign in to comment.