From cec83f7d2e8025a35a2a95cce6a8346c81a4b76d Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Mon, 1 Jun 2015 22:12:25 +0800 Subject: [PATCH] debugger: improve ESRCH error message When use `iojs debug -p ` with an invalid pid, the debugger print internal error message also, it not enough smart. --- lib/_debugger.js | 15 +++++++++--- test/debugger/test-debugger-pid.js | 39 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 test/debugger/test-debugger-pid.js diff --git a/lib/_debugger.js b/lib/_debugger.js index 4c503d2f5480e9..d13c47f1023daf 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -1639,7 +1639,16 @@ Interface.prototype.trySpawn = function(cb) { } else if (this.args.length === 3) { // `node debug -p pid` if (this.args[1] === '-p' && /^\d+$/.test(this.args[2])) { - process._debugProcess(parseInt(this.args[2], 10)); + const pid = parseInt(this.args[2], 10); + try { + process._debugProcess(pid); + } catch (e) { + if (e.code === 'ESRCH') { + console.error('Target process: ' + pid + ' doesn\'t exist.'); + process.exit(1); + } + throw e; + } isRemote = true; } else { var match = this.args[1].match(/^--port=(\d+)$/); @@ -1704,8 +1713,8 @@ Interface.prototype.trySpawn = function(cb) { function connectError() { // If it's failed to connect 10 times then print failed message if (connectionAttempts >= 10) { - self.stdout.write(' failed, please retry\n'); - return; + console.error(' failed, please retry'); + process.exit(1); } setTimeout(attemptConnect, 500); } diff --git a/test/debugger/test-debugger-pid.js b/test/debugger/test-debugger-pid.js new file mode 100644 index 00000000000000..bb81b308098f4e --- /dev/null +++ b/test/debugger/test-debugger-pid.js @@ -0,0 +1,39 @@ +'use strict'; +var common = require('../common'); +var assert = require('assert'); +var spawn = require('child_process').spawn; + +var port = common.PORT + 1337; +var buffer = ''; +var expected = []; +var scriptToDebug = common.fixturesDir + '/empty.js'; + +function fail() { + assert(0); // `--debug-brk script.js` should not quit +} + +// connect to debug agent +var interfacer = spawn(process.execPath, ['debug', '-p', '655555']); + +console.error(process.execPath, 'debug', '-p', '655555'); +interfacer.stdout.setEncoding('utf-8'); +interfacer.stderr.setEncoding('utf-8'); +var onData = function (data) { + data = (buffer + data).split('\n'); + buffer = data.pop(); + data.forEach(function(line) { + interfacer.emit('line', line); + }); +}; +interfacer.stdout.on('data', onData); +interfacer.stderr.on('data', onData); + +interfacer.on('line', function(line) { + line = line.replace(/^(debug> *)+/, ''); + var expected = 'Target process: 655555 doesn\'t exist.'; + assert.ok(expected == line, 'Got unexpected line: ' + line); +}); + +interfacer.on('exit', function (code, signal) { + assert.ok(code == 1, 'Got unexpected code: ' + code); +});