Skip to content

Clearer exception messages. #84

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

Merged
merged 4 commits into from
Nov 25, 2016
Merged

Clearer exception messages. #84

merged 4 commits into from
Nov 25, 2016

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Nov 17, 2016

Q A
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? yes
Related tickets na
Documentation php-http/documentation#165
License MIT

Relates to mailgun/mailgun-php#222

If discovery fails we get an error like:

Fatal error: Uncaught Http\Discovery\Exception\PuliUnavailableException: Puli Factory is not available in /Users/tobias/Workspace/PHPStorm/test/vendor/php-http/discovery/src/MessageFactoryDiscovery.php on line 27

Http\Discovery\NotFoundException: No message factories found. To use Guzzle, Diactoros or Slim Framework factories install php-http/message and the chosen message implementation. in /Users/tobias/Workspace/PHPStorm/test/vendor/php-http/discovery/src/MessageFactoryDiscovery.php on line 27

Call Stack:
    0.0006     353240   1. {main}() /Users/tobias/Workspace/PHPStorm/test/index.php:0
    0.0023     506296   2. Mailgun\Mailgun->get() /Users/tobias/Workspace/PHPStorm/test/index.php:6
    0.0023     506352   3. Mailgun\Connection\RestClient->get() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php:129
    0.0024     506384   4. Mailgun\Connection\RestClient->send() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:167

Which leads users to think ""wtf is Puli?"..


With this PR we get an exception like this instead. This is more descriptive:

Fatal error: Uncaught Http\Discovery\Exception\DiscoveryFailedException: Could not find resource using any discovery strategy. Find more information at http://docs.php-http.org/en/latest/discovery.html#common-errors
 - Puli Factory is not available

 in /Users/tobias/Workspace/PHPStorm/test/vendor/php-http/discovery/src/MessageFactoryDiscovery.php on line 27

Http\Discovery\NotFoundException: No message factories found. To use Guzzle, Diactoros or Slim Framework factories install php-http/message and the chosen message implementation. in /Users/tobias/Workspace/PHPStorm/test/vendor/php-http/discovery/src/MessageFactoryDiscovery.php on line 27

Call Stack:
    0.0005     353240   1. {main}() /Users/tobias/Workspace/PHPStorm/test/index.php:0
    0.0023     506296   2. Mailgun\Mailgun->get() /Users/tobias/Workspace/PHPStorm/test/index.php:6
    0.0023     506352   3. Mailgun\Connection\RestClient->get() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php:129
    0.0023     506384   4. Mailgun\Connection\RestClient->send() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:167
    0.0028     531816   5. Http\Discovery\MessageFactoryDiscovery::find() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:96

When php-http/message and a PSR7 is installed and I try to rerun I get an exception like:

Fatal error: Uncaught Http\Discovery\Exception\DiscoveryFailedException: Could not find resource using any discovery strategy. Find more information at http://docs.php-http.org/en/latest/discovery.html#common-errors
 - Puli Factory is not available

 in /Users/tobias/Workspace/PHPStorm/test/vendor/php-http/discovery/src/HttpClientDiscovery.php on line 27

Http\Discovery\NotFoundException: No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter". in /Users/tobias/Workspace/PHPStorm/test/vendor/php-http/discovery/src/HttpClientDiscovery.php on line 27

Call Stack:
    0.0007     353240   1. {main}() /Users/tobias/Workspace/PHPStorm/test/index.php:0
    0.0031     631456   2. Mailgun\Mailgun->get() /Users/tobias/Workspace/PHPStorm/test/index.php:6
    0.0031     631512   3. Mailgun\Connection\RestClient->get() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php:129
    0.0031     631544   4. Mailgun\Connection\RestClient->send() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:167
    0.0070     907896   5. Mailgun\Connection\RestClient->getHttpClient() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:97
    0.0072     911024   6. Http\Discovery\HttpClientDiscovery::find() /Users/tobias/Workspace/PHPStorm/test/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:291

@Nyholm Nyholm changed the title [WIP] Clearer exception messages. Clearer exception messages. Nov 18, 2016
@Nyholm
Copy link
Member Author

Nyholm commented Nov 18, 2016

Im done with this PR.
I also added more documentation about this: php-http/documentation#165

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

*/
public function __construct($message, array $exceptions = [])
{
$this->exceptions = $exceptions;

parent::__construct($message, 0, array_shift($exceptions));
parent::__construct($message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is eating away the chance to see a root cause exception. in general, it could be useful to see where the original problem comes from. but in the situation i agree it would be more confusing than helpful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been back and forth with this. But I believe this is the best solution.

Say that you have 5 different strategies. 4 of them fails (StrategyUnavailable), The exception message for the first exception will say "Puli factory not found". But your intention was to use "FoobarStrategy". So "Puli" will be confusing.

The new message will be:

Fatal error: Uncaught Http\Discovery\Exception\DiscoveryFailedException: Could not find resource using any discovery strategy. Find more information at http://docs.php-http.org/en/latest/discovery.html#common-errors
 - Puli Factory is not available
 - Foobar strategy is not available
 - Something else. 

@dbu
Copy link
Contributor

dbu commented Nov 18, 2016 via email

@Nyholm
Copy link
Member Author

Nyholm commented Nov 25, 2016

I added a change log

@dbu dbu merged commit dd332f3 into master Nov 25, 2016
@dbu dbu deleted the exception-message branch November 25, 2016 13:53
@sagikazarmark
Copy link
Member

Looks good to me

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

Successfully merging this pull request may close these issues.

3 participants