Skip to content

Commit

Permalink
feat: show a sensible error message if the address cannot be found (#46)
Browse files Browse the repository at this point in the history
This handles ENOTFOUND errors and ensures that if the address is
unresolvable we fail-fast with a meaningful error.
  • Loading branch information
dwmkerr authored Oct 3, 2019
1 parent 21bf83c commit b980aa8
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ The following error codes are returned:
| Code | Meaning |
|------|---------|
| `0` | The specified port on the host is accepting connections. |
| `1` | A timeout occured waiting for the port to open. |
| `2` | Un unknown error occured waiting for the port to open. The program cannot establish whether the port is open or not. |
| `1` | A timeout occurred waiting for the port to open. |
| `2` | An unknown error occurred waiting for the port to open. The program cannot establish whether the port is open or not. |
| `3` | The address cannot be found (e.g. no DNS entry, or unresolvable). |
r

# API

Expand Down
8 changes: 6 additions & 2 deletions bin/wait-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const debug = require('debug')('wait-port');
const program = require('commander');
const pkg = require('../package.json');
const extractTarget = require('../lib/extract-target');
const ValidationError = require('../lib/validation-error');
const ValidationError = require('../lib/errors/validation-error');
const ConnectionError = require('../lib/errors/connection-error');
const waitPort = require('../lib/wait-port');

program
Expand Down Expand Up @@ -38,8 +39,11 @@ program
.catch((err) => {
// Show validation errors in red.
if (err instanceof ValidationError) {
console.error(chalk.red(err.message));
console.error(`\n\n ${chalk.red(err.message)}`);
process.exit(2);
} else if (err instanceof ConnectionError) {
console.error(`\n\n ${chalk.red(err.message)}`);
process.exit(4);
} else {
console.error(`Unknown error occurred waiting for ${target}: ${err}`);
process.exit(3);
Expand Down
9 changes: 9 additions & 0 deletions lib/errors/connection-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ConnectionError extends Error {
constructor(message) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = 'ConnectionError';
}
}

module.exports = ConnectionError;
1 change: 1 addition & 0 deletions lib/validation-error.js → lib/errors/validation-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ValidationError extends Error {
constructor(message) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = 'ValidationError';
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/validate-parameters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const outputFunctions = require('./output-functions');
const ValidationError = require('./validation-error');
const ValidationError = require('./errors/validation-error');

function validateParameters(params) {
// Coerce the protocol.
Expand Down
7 changes: 7 additions & 0 deletions lib/wait-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const debug = require('debug')('wait-port');
const net = require('net');
const outputFunctions = require('./output-functions');
const validateParameters = require('./validate-parameters');
const ConnectionError = require('./errors/connection-error');

function createConnectionWithTimeout({ host, port }, timeout, callback) {
// Variable to hold the timer we'll use to kill the socket if we don't
Expand Down Expand Up @@ -104,6 +105,12 @@ function tryConnect(options, timeout) {
debug('Socket not open: ECONNRESET');
socket.destroy();
return resolve(false);
} else if (err.code === 'ENOTFOUND') {
// This will occur if the address is not found, i.e. due to a dns
// lookup fail (normally a problem if the domain is wrong).
debug('Socket cannot be opened: ENOTFOUND');
socket.destroy();
return reject(new ConnectionError(`The address '${options.host}' cannot be found`));
}

// Trying to open the socket has resulted in an error we don't
Expand Down
14 changes: 14 additions & 0 deletions lib/wait-port.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ describe('wait-port', () => {
});
});

it('should error if the address is not found', () => {

// Wait for a point on an address (I hope) does not exist.
return waitPort({ host: 'ireallyhopethatthisdomainnamedoesnotexist.com', port: 9021, timeout: 3000, output: 'silent' })
.then(() => {
assert.fail('The operation should throw, rather than completing.');
})
.catch((err) => {
assert.equal(err.name, 'ConnectionError', 'A ConnectionFailed error should be thrown');
assert(/.*address.*ireallyhopethatthisdomainnamedoesnotexist.com/.test(err.message));
});
});


it('should successfully wait for a valid http response', () => {
const server = http.createServer((req, res) => {
res.writeHead(200);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"man": "./man/wait-port.1",
"scripts": {
"lint": "eslint .",
"test": "DEBUG=wait-port nyc --report-dir 'artifacts/coverage' -x 'lib/**/*.spec.js' --reporter=html --reporter=text mocha --recursive -t 10000 'lib/**/*.spec.js' --reporter mocha-junit-reporter --reporter-options mochaFile=./artifacts/test-reports/test-results.xml",
"test": "DEBUG=wait-port nyc --report-dir 'artifacts/coverage' -x 'lib/**/*.spec.js' --reporter=html --reporter=text mocha --recursive -t 10000 'lib/**/*.spec.js'",
"test:ci": "DEBUG=wait-port nyc --report-dir 'artifacts/coverage' -x 'lib/**/*.spec.js' --reporter=html --reporter=text mocha --recursive -t 10000 'lib/**/*.spec.js' --reporter mocha-junit-reporter --reporter-options mochaFile=./artifacts/test-reports/test-results.xml",
"test:watch": "DEBUG=wait-port mocha --watch --recursive -t 10000 'lib/**/*.spec.js'",
"report-coverage": "nyc report --reporter=text-lcov > ./artifacts/coverage/coverage.lcov && codecov",
"debug": "DEBUG=wait-port mocha --recursive --inspect --debug-brk 'lib/**/*.spec.js'"
Expand Down

0 comments on commit b980aa8

Please sign in to comment.