Core\Process\Handle::readStderr
$handle->readStderr(): ?bytesReads the error output of a program that Core\Process::spawn started, while the program still runs. Programs write their error messages and warnings to this output, which is called standard error. Each call waits until the program writes something there, and returns it as bytes. Use as string to convert it to text.
It works like readStdout. The two outputs are separate, so an error message never appears in what readStdout returns. Call readStderr() in a loop to read everything. At the end of the error output the result is null. Each part is tainted bytes, as in readStdout.
The examples below read every warning a program writes, keep a result apart from a note about it, and show why a job failed.
Return value
Returns ?bytes. The next part of the error output, as tainted bytes, up to 64 KiB. At the end of the error output the result is null, and every later call also returns null. Use as string to convert a part to text.
Errors
TimeoutErrorThetimeoutgiven tospawnhas passed. The program is stopped, and every later call exceptkillthrows this error too.IOErrorThe operating system could not read the error output. The error output is then closed, so the next call returnsnull.
Changelog
| Version | Change |
|---|---|
0.0.6 | The output of a child process is tainted bytes. The version text of a database server and the name an LDAP server returns are tainted string. Another program or a server writes this text, so it may contain input from outside. |
Examples
Reading every warning
<?nvs// Shows how to read everything a program writes to its error output.// requires: unix
// This program writes two warnings to its error output, and nothing else.var $program = Core\Process::spawn( '/bin/sh', ['-c', 'echo "warning: the disk is almost full" >&2; echo "warning: the last backup is old" >&2'],);
tainted string $warnings = '';// `readStderr()` returns the next part of the error output. At the end, it returns `null`.?tainted bytes $part = $program->readStderr();while ($part != null) { $warnings = $warnings . ($part as string); $part = $program->readStderr();}$program->wait();
// This prints both warnings, one on each line.echo $warnings;warning: the disk is almost fullwarning: the last backup is oldA result and a note
<?nvs// Shows that the output and the error output of a program are kept apart.// requires: unix
// This program writes its result to the output, and a note to the error output.var $program = Core\Process::spawn( '/bin/sh', ['-c', 'echo "42"; echo "note: this took 3 tries" >&2'],);
// `readStdout()` returns only the result. The note is not in it.tainted string $result = '';?tainted bytes $part = $program->readStdout();while ($part != null) { $result = $result . ($part as string); $part = $program->readStdout();}
// `readStderr()` returns only the note.tainted string $note = '';$part = $program->readStderr();while ($part != null) { $note = $note . ($part as string); $part = $program->readStderr();}$program->wait();
// This prints `result: 42` and then the note.echo 'result: ', $result;echo 'error output: ', $note;result: 42error output: note: this took 3 triesShowing why a job failed
<?nvs// Shows how to report why a job failed, with the error message the job wrote.// requires: unix
// This job prints its progress, then fails with an error message and exit code 1.var $job = Core\Process::spawn( '/bin/sh', ['-c', 'echo "copying 3 files"; echo "cannot copy report.pdf: permission denied" >&2; exit 1'],);
// The progress is printed as it arrives.?tainted bytes $part = $job->readStdout();while ($part != null) { echo ($part as string); $part = $job->readStdout();}
// The error message is kept, so it can be shown if the job fails.tainted string $message = '';$part = $job->readStderr();while ($part != null) { $message = $message . ($part as string); $part = $job->readStderr();}
// The exit code is 1, so this prints the error message.int $code = $job->wait()->exitCode();if ($code != 0) { echo 'the job failed with exit code ', $code, ': ', $message;}copying 3 filesthe job failed with exit code 1: cannot copy report.pdf: permission denied