Browse files

Helpful error when child_process.exec hit maxBuffer

  • Loading branch information...
1 parent 1f041fe commit cb06abe1e586217e31907d792ef3b967936e3f77 @ry ry committed Jan 28, 2011
Showing with 17 additions and 1 deletion.
  1. +7 −1 lib/child_process.js
  2. +10 −0 test/simple/test-exec-max-buffer.js
View
8 lib/child_process.js
@@ -60,6 +60,8 @@ exports.execFile = function(file /* args, options, callback */) {
var exited = false;
var timeoutId;
+ var err;
+
function exithandler(code, signal) {
if (exited) return;
exited = true;
@@ -71,7 +73,9 @@ exports.execFile = function(file /* args, options, callback */) {
if (!callback) return;
- if (code === 0 && signal === null) {
+ if (err) {
+ callback(err, stdout, stderr);
+ } else if (code === 0 && signal === null) {
callback(null, stdout, stderr);
} else {
var e = new Error('Command failed: ' + stderr);
@@ -103,13 +107,15 @@ exports.execFile = function(file /* args, options, callback */) {
child.stdout.addListener('data', function(chunk) {
stdout += chunk;
if (stdout.length > options.maxBuffer) {
+ err = new Error('maxBuffer exceeded.');
kill();
}
});
child.stderr.addListener('data', function(chunk) {
stderr += chunk;
if (stderr.length > options.maxBuffer) {
+ err = new Error('maxBuffer exceeded.');
kill();
}
});
View
10 test/simple/test-exec-max-buffer.js
@@ -0,0 +1,10 @@
+var common = require('../common');
+var exec = require('child_process').exec;
+var assert = require('assert');
+
+var cmd = 'echo "hello world"';
+
+exec(cmd, { maxBuffer: 5 }, function(err, stdout, stderr) {
+ assert.ok(err);
+ assert.ok(/maxBuffer/.test(err.message));
+});

0 comments on commit cb06abe

Please sign in to comment.