Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 873ba01

Browse files
authored
Merge pull request #62 from nodejs/jk-handle-blocked-port
Handle blocked port
2 parents 3c9073f + d278b23 commit 873ba01

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
registry=https://registry.npmjs.org
2+
package-lock=false

lib/_inspect.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ const [ InspectClient, createRepl ] =
4242

4343
const debuglog = util.debuglog('inspect');
4444

45+
class StartupError extends Error {
46+
constructor(message) {
47+
super(message);
48+
this.name = 'StartupError';
49+
}
50+
}
51+
4552
function portIsFree(host, port, timeout = 2000) {
4653
if (port === 0) return Promise.resolve(); // Binding to a random port.
4754

@@ -51,7 +58,7 @@ function portIsFree(host, port, timeout = 2000) {
5158
return new Promise((resolve, reject) => {
5259
setTimeout(() => {
5360
didTimeOut = true;
54-
reject(new Error(
61+
reject(new StartupError(
5562
`Timeout (${timeout}) waiting for ${host}:${port} to be free`));
5663
}, timeout);
5764

@@ -346,10 +353,14 @@ function startInspect(argv = process.argv.slice(2),
346353
stdin.resume();
347354

348355
function handleUnexpectedError(e) {
349-
console.error('There was an internal error in node-inspect. ' +
350-
'Please report this bug.');
351-
console.error(e.message);
352-
console.error(e.stack);
356+
if (!(e instanceof StartupError)) {
357+
console.error('There was an internal error in node-inspect. ' +
358+
'Please report this bug.');
359+
console.error(e.message);
360+
console.error(e.stack);
361+
} else {
362+
console.error(e.message);
363+
}
353364
if (inspector.child) inspector.child.kill();
354365
process.exit(1);
355366
}

test/cli/invalid-args.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
'use strict';
2+
const Path = require('path');
3+
const { createServer } = require('net');
4+
25
const { test } = require('tap');
36

47
const startCLI = require('./start-cli');
@@ -23,3 +26,29 @@ test('launch w/ invalid host:port', (t) => {
2326
t.equal(code, 1, 'exits with non-zero exit code');
2427
});
2528
});
29+
30+
test('launch w/ unavailable port', async (t) => {
31+
const blocker = createServer(socket => socket.end());
32+
const port = await new Promise((resolve, reject) => {
33+
blocker.on('error', reject);
34+
blocker.listen(0, '127.0.0.1', () => resolve(blocker.address().port));
35+
});
36+
37+
try {
38+
const script = Path.join('examples', 'three-lines.js');
39+
const cli = startCLI([`--port=${port}`, script]);
40+
const code = await cli.quit();
41+
42+
t.notMatch(
43+
cli.output,
44+
'report this bug',
45+
'Omits message about reporting this as a bug');
46+
t.match(
47+
cli.output,
48+
`waiting for 127.0.0.1:${port} to be free`,
49+
'Tells the user that the port wasn\'t available');
50+
t.equal(code, 1, 'exits with non-zero exit code');
51+
} finally {
52+
blocker.close();
53+
}
54+
});

0 commit comments

Comments
 (0)