Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamDecoratorTrait::__toString could cause fatal errors with trigger_error #180

Closed
origaminal opened this issue Dec 28, 2017 · 2 comments

Comments

@origaminal
Copy link

origaminal commented Dec 28, 2017

There is the work around in StreamDecoratorTrait::__toString method for inability of __toString to throw exceptions.

public function __toString()
    {
        try {
            if ($this->isSeekable()) {
                $this->seek(0);
            }
            return $this->getContents();
        } catch (\Exception $e) {
            // Really, PHP? https://bugs.php.net/bug.php?id=53648
            trigger_error('StreamDecorator::__toString exception: '
                . (string) $e, E_USER_ERROR);
            return '';
        }
    }

The problem that there could be user-defined handlers that throws Exceptions on their own like it was in 2.7 version of Symfony

https://github.com/symfony/symfony/blob/ff630e19bbdea66d972a969445ecc2396c754aac/src/Symfony/Component/Debug/ErrorHandler.php#L399-L415

This leads to the Fatal Error which is quite tricky to debug.

Whether there could be any better strategy for this piece of code? Maybe to wrap trigger_error in a try-catch block and do return '' if an exception was thrown? Or at least to add the comment like if you still do get "FatalError ::__toString must not throw an exception" it is cause your error handler throw an exception - this could save some time in debugging.

@sagikazarmark
Copy link
Member

Is the fatal error saying that __toString should not throw exceptions? Can you please provide more information (code, error messages, etc), what you exactly see? Thanks!

@Tobion
Copy link
Member

Tobion commented Jun 6, 2019

Closing as the problem will be solved for upcoming php versions #271

@Tobion Tobion closed this as completed Jun 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants