diff --git a/CHANGELOG.md b/CHANGELOG.md index a1d7a28..ba81f88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/ClassDiscovery.php b/src/ClassDiscovery.php index 08ad375..b10e770 100644 --- a/src/ClassDiscovery.php +++ b/src/ClassDiscovery.php @@ -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); } /** diff --git a/src/Exception/DiscoveryFailedException.php b/src/Exception/DiscoveryFailedException.php index 6f2164f..304b727 100644 --- a/src/Exception/DiscoveryFailedException.php +++ b/src/Exception/DiscoveryFailedException.php @@ -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); } /** - * @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() {