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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Changed

- Made exception messages clearer. `StrategyUnavailableException` is no longer the previous exception to `DiscoveryFailedException`.
- `CommonClassesStrategy` is using `self` instead of `static`. Using `static` makes no sense when `CommonClassesStrategy` is final.

## 1.1.0 - 2016-10-20
Expand Down
2 changes: 1 addition & 1 deletion src/ClassDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected static function findOneByType($type)
}
}

throw new DiscoveryFailedException('Could not find resource using any discovery strategy', $exceptions);
throw DiscoveryFailedException::create($exceptions);
}

/**
Expand Down
23 changes: 19 additions & 4 deletions src/Exception/DiscoveryFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@
final class DiscoveryFailedException extends \Exception implements Exception
{
/**
* @var array
* @var \Exception[]
*/
private $exceptions;

/**
* @param $exceptions
* @param string $message
* @param \Exception[] $exceptions
*/
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. 

}

/**
* @return array
* @param \Exception[] $exceptions
*/
public static function create($exceptions)
{
$message = 'Could not find resource using any discovery strategy. Find more information at http://docs.php-http.org/en/latest/discovery.html#common-errors';
foreach ($exceptions as $e) {
$message .= "\n - ".$e->getMessage();
}
$message .= "\n\n";

return new self($message, $exceptions);
}

/**
* @return \Exception[]
*/
public function getExceptions()
{
Expand Down