Skip to content

Commit 81029c6

Browse files
JacksonTianbnoordhuis
authored andcommitted
debugger: improve ESRCH error message
When using `iojs debug -p <pid>` with an invalid pid, the debugger printed an internal error message because it wasn't smart enough to figure out that the target process didn't exist. Now it is. PR-URL: #1863 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Yosuke Furukawa <[email protected]>
1 parent 353e26e commit 81029c6

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

lib/_debugger.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,16 @@ Interface.prototype.trySpawn = function(cb) {
16391639
} else if (this.args.length === 3) {
16401640
// `node debug -p pid`
16411641
if (this.args[1] === '-p' && /^\d+$/.test(this.args[2])) {
1642-
process._debugProcess(parseInt(this.args[2], 10));
1642+
const pid = parseInt(this.args[2], 10);
1643+
try {
1644+
process._debugProcess(pid);
1645+
} catch (e) {
1646+
if (e.code === 'ESRCH') {
1647+
console.error(`Target process: ${pid} doesn't exist.`);
1648+
process.exit(1);
1649+
}
1650+
throw e;
1651+
}
16431652
isRemote = true;
16441653
} else {
16451654
var match = this.args[1].match(/^--port=(\d+)$/);
@@ -1704,8 +1713,8 @@ Interface.prototype.trySpawn = function(cb) {
17041713
function connectError() {
17051714
// If it's failed to connect 10 times then print failed message
17061715
if (connectionAttempts >= 10) {
1707-
self.stdout.write(' failed, please retry\n');
1708-
return;
1716+
console.error(' failed, please retry');
1717+
process.exit(1);
17091718
}
17101719
setTimeout(attemptConnect, 500);
17111720
}

test/debugger/test-debugger-pid.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
var common = require('../common');
3+
var assert = require('assert');
4+
var spawn = require('child_process').spawn;
5+
6+
var port = common.PORT + 1337;
7+
var buffer = '';
8+
var expected = [];
9+
var scriptToDebug = common.fixturesDir + '/empty.js';
10+
11+
function fail() {
12+
assert(0); // `--debug-brk script.js` should not quit
13+
}
14+
15+
// connect to debug agent
16+
var interfacer = spawn(process.execPath, ['debug', '-p', '655555']);
17+
18+
console.error(process.execPath, 'debug', '-p', '655555');
19+
interfacer.stdout.setEncoding('utf-8');
20+
interfacer.stderr.setEncoding('utf-8');
21+
var onData = function(data) {
22+
data = (buffer + data).split('\n');
23+
buffer = data.pop();
24+
data.forEach(function(line) {
25+
interfacer.emit('line', line);
26+
});
27+
};
28+
interfacer.stdout.on('data', onData);
29+
interfacer.stderr.on('data', onData);
30+
31+
interfacer.on('line', function(line) {
32+
line = line.replace(/^(debug> *)+/, '');
33+
var expected = 'Target process: 655555 doesn\'t exist.';
34+
assert.ok(expected == line, 'Got unexpected line: ' + line);
35+
});
36+
37+
interfacer.on('exit', function(code, signal) {
38+
assert.ok(code == 1, 'Got unexpected code: ' + code);
39+
});

0 commit comments

Comments
 (0)