Skip to content

clarify getStderr behaviour when STDERR is not available #112

@azjezz

Description

@azjezz

currently, ByteStream\getStderr has a weird behaviour in my opnion when the standard error output is not available. which is that ByteStream\getStderr() returns a stream, but, it is not the expected STDERR stream, instead, its a php://memory stream that has been closed.

I would expect either:

  1. return null indicating that the stream is not available
  2. throw an exception informing me that the stream is not available

It would be good if this behaviour could be clarified maybe via documentation. and maybe introducing a new function that has a ( IMO ) saner behaviour?


For context, i was trying to replicate this:

    /**
     * Returns the default terminal output.
     *
     * @internal
     */
    public static function getOutput(): OutputInterface
    {
        $standardOutputHandle = IO\output_handle();
        $standardErrorOutputHandle = IO\error_handle();
        if (null === $standardErrorOutputHandle) {
            return new HandleOutput($standardOutputHandle);
        }

        return new HandleConsoleOutput($standardOutputHandle, $standardErrorOutputHandle);
    }

which is possible to replicate with the current behaviour:

    /**
     * Returns the default terminal output.
     *
     * @internal
     */
    public static function getOutput(): OutputInterface
    {
        $standardOutputStream = ByteStream\getStdout();
        $standardErrorOutputStream = ByteStream\getStderr();
        if ($standardErrorOutputStream->isClosed()) {
            return new ByteStreamOutput($standardOutputStream);
        }

        return new ByteStreamConsoleOutput($standardOutputStream, $standardErrorOutputStream);
    }

however, this implementation took me a bit of time to figure out as Amp does not mention anything about returning a closed stream from getStderr.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions