Phantomjs: Does console.error writes to stderr ?

Created on 8 Feb 2014  路  3Comments  路  Source: ariya/phantomjs

Hello,

Using lastest phantomjs 1 9 7, i can t manage to write to stderr using console.err.

In the front end script

  throw "some errors";

In thephantomjs script

  page.onConsoleMessage = function(msg, lineNum, sourceId) {
    console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
  };

  page.onError = function(msg, trace) {
    var msgStack = ['ERROR: ' + msg];
    if (trace && trace.length) {
      msgStack.push('TRACE:');
      trace.forEach(function(t) {
        msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
      });
    }
    console.error(msgStack.join('\n'));
  };

this works

 var phantomjsprocess = require('child_process').execFile(phantomjs.path, childArgs);
      phantomjsprocess.stdout
        .on('data', function (data) {
          grunt.verbose.writeln(data.trim());
        })
        // having some difficuties to pass phantomjs errors to stderr, so listens to stdout for errors
        .on('data', function (data) {
          if( data.match(/^(ERROR: )/) ){
            grunt.log.writeln(data.trim());
          }
        });

this does not, or seems not.

 var phantomjsprocess = require('child_process').execFile(phantomjs.path, childArgs);
      phantomjsprocess.stderr
        .on('data', function (data) {
          grunt.log.writeln(data.trim());
        });

I read the issue #10232 it is exactly my problem, but so far, i can t get it working.

Do you have any hints ?

thanks.

Most helpful comment

Oh sorry i mised it... Thanks for pointing.

For next people,
add this snippet into the phantomjs wrapper

console.error = function () {
    require("system").stderr.write(Array.prototype.join.call(arguments, ' ') + '\n');
};

Then listen to the stderr of your child process.

Tested on phantomJS1.9.7@ubuntu13

Works like a charm !

All 3 comments

Oh sorry i mised it... Thanks for pointing.

For next people,
add this snippet into the phantomjs wrapper

console.error = function () {
    require("system").stderr.write(Array.prototype.join.call(arguments, ' ') + '\n');
};

Then listen to the stderr of your child process.

Tested on phantomJS1.9.7@ubuntu13

Works like a charm !

@maboiteaspam I stole your snippet and threw it in https://github.com/sotownsend/BooJS for stderr by default on console.error.

Was this page helpful?
0 / 5 - 0 ratings