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

PHP Fatal error: Method GuzzleHttp\Psr7\Uri::__toString() must not throw an exception #29

Closed
curtis1000 opened this issue Jul 31, 2015 · 2 comments

Comments

@curtis1000
Copy link

Hello, I'm running into this fatal error with one of my tests:

PHP Fatal error:  Method GuzzleHttp\Psr7\Uri::__toString() must not throw an exception in /path-to/test/TestCase.php on line 0

It appears to stem from a call to GuzzleHttp\Client::getAsync(), though wrapping that call in a try/catch doesn't seem to catch an exception.

Thanks

@mtdowling
Copy link
Member

The toString method of the Uri class doesn't throw exceptions. It just calls this method: https://github.com/guzzle/psr7/blob/master/src/Uri.php#L480. I don't see how it would have directly called anything from the Guzzle client because this project doesn't know about Guzzle. Can you show what your test does?

@elazar
Copy link

elazar commented Sep 25, 2015

For posterity:

The issue ended up not being with Guzzle at all, but was due to how our test suite was set up.

In our subclass of PHPUnit_Framework_TestCase, we were overriding tearDown() and from there calling GuzzleHttp\Promise\unwrap() with an array of GuzzleHttp\Promise\PromiseInterface instances obtained from calling GuzzleHttp\Client->*Async() methods.

When Guzzle encounters an error condition (e.g. a response with a 401 status code), it throws an instance of GuzzleHttp\Exception\ClientException. We had a try / catch block in tearDown() to handle this condition by obtaining relevant debugging information and throwing a new RuntimeException instance.

The actual problem was that this catch block was causing a PHP notice to be emitted. If we excluded E_NOTICE from the error_reporting setting, the tests ran without encountering the condition originally reported in this issue, which appeared to happen partly due to a behavior of PHPUnit.

By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception.

https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.errors

By calling restore_error_handler() in setUp() in our test case class, we were able to deactivate PHPUnit's custom error handler, revealing the PHP notice being emitted, which as it turns out was completely unrelated to Uri::__toString().

I'm not altogether certain why the error originally reported here was the result of all of this. All attempts to debug an issue in Uri::__toString() -- adding a try / catch block there, outputting backtraces, etc. failed because the issue simply wasn't with that area of code.

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