getting data but also an error
Opened this issue · 1 comments
I am not sure, but may this be a bug?
I have a very simple script:
function testingStrangeThing(promObj) {
return new Promise((resolve, reject) => {
try {
cmd.get(
'ls',
function(error, data, stderr) {
if(error) {
console.log("error first"); console.log(error);
console.log('');
console.log('data'); console.log(data);
console.log('');
console.log('stderr'); console.log(stderr);
reject(error)
}
else if(data) {
console.log("God data");
resolve(data);
}
else {
throw 'No error and no data as response of gdallocationinfo';
}
}
);} catch(error) { reject(error); }
});
};
It logs the content of the current directory correctly, but also writes this error:
error first
{ Error: Command failed: ls
at ChildProcess.exithandler (child_process.js:272:12)
at ChildProcess.emit (events.js:159:13)
at maybeClose (internal/child_process.js:943:16)
at Socket.stream.socket.on (internal/child_process.js:363:11)
at Socket.emit (events.js:159:13)
at Socket.ReaddirReadable.destroy (/home/n_trza01/Geosoft2DiscoveryService/node_modules/fs-readdir/index.js:159:8)
at Socket.onSocketFinish (net.js:279:17)
at Socket.emit (events.js:164:20)
at finishMaybe (_stream_writable.js:605:14)
at endWritable (_stream_writable.js:613:3) killed: false, code: null, signal: null, cmd: 'ls' }
I am using node.js version 9.2.1.
const cmd = require("node-cmd");
function testingStrangeThing(promObj) {
return new Promise((resolve, reject) => {
try {
cmd.get(
'ls',
function(error, data, stderr) {
if(error) {
console.log("error first"); console.log(error);
console.log('');
console.log('data'); console.log(data);
console.log('');
console.log('stderr'); console.log(stderr);
reject(error)
}
else if(data) {
console.log("God data");
resolve(data);
}
else {
throw 'No error and no data as response of gdallocationinfo';
}
}
);
}
catch(error) {
reject(error);
}
});
};
async function foo() {
let x = await testingStrangeThing(3);
console.log(x);
}
foo();
I tested your code with the above code on v8.9.3. I cannot reproduce the issue. But according to nodejs/node-v0.x-archive#4590 (comment) it seems like exec (the command from child_process used in node-cmd) can report false errors.
If you do get a false error, you will have to check the error code.
if (error && error.code == 2) {
}
Or something along the lines of that.