Core\Process\Result::stderr
$result->stderr(): bytesReturns everything a program wrote to its standard error. Programs write their error messages and warnings there, and their normal results to the standard output. The two outputs are kept apart, so a warning is never mixed into the data that stdout returns.
The result is tainted bytes, like the result of stdout. Use as string to convert it to text, and write tainted string on the variable. When the program wrote no errors, the result is empty.
A good time to read this output is when exitCode is not 0. The message usually says what went wrong, so you can put it in your own error or in a log.
The examples below read the two outputs of one program, check that a program wrote no warnings, and put a failed tool's message in an error.
Return value
Returns bytes. The error output, as tainted bytes. Use as string to convert it to text.
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
Two outputs kept apart
<?nvs// Shows that a program's results and its warnings arrive in two separate outputs.// requires: unix
// The script prints a result, then a warning with `>&2`, then a second result.var $result = Core\Process::run('/bin/sh', ['-c', "echo 12; echo 'warning: slow disk' >&2; echo 30"]);
// `stdout()` has only the two results. This prints `12` and `30`.echo $result->stdout() as string;
// `stderr()` has only the warning. This prints `warning: slow disk`.echo $result->stderr() as string;1230warning: slow diskA run without warnings
<?nvs// Shows how to check that a program wrote no warnings.// requires: unix
array<string> $scripts = ['echo done', "echo done; echo 'warning: old settings' >&2"];
foreach ($scripts as string $script) { tainted bytes $warnings = Core\Process::run('/bin/sh', ['-c', $script])->stderr(); // An empty error output has length `0`. if (Core\Bytes::length($warnings) == 0) { echo "no warnings\n"; } else { echo 'warnings: ', $warnings as string; }}no warningswarnings: warning: old settingsThe message of a failed tool
<?nvs// Shows a method that throws an error with the tool's own message when the tool fails.// requires: unix
class Tools{ public static function run(string $path, array<string> $arguments): tainted string { var $result = Core\Process::run($path, $arguments); if ($result->exitCode() != 0) { // The tool's message says what went wrong. `Core\Str::trim` removes the new line. tainted string $message = Core\Str::trim($result->stderr() as string); // `Core\Cli::escape` makes the message safe to print in a terminal. throw new RuntimeError('the tool failed: ' . Core\Cli::escape($message)); } return $result->stdout() as string; }}
// This folder has three folders, so `ls` prints their names.echo Tools::run('/bin/ls', ['docs/examples/core/Process-Result']);
// This shell script writes a message to stderr and fails. The error contains that message.try { echo Tools::run('/bin/sh', ['-c', "echo 'the disk is full' >&2; exit 1"]);} catch (RuntimeError $failed) { echo $failed->message, "\n";}exitCodestderrstdoutthe tool failed: the disk is full