Skip to content

Commit

Permalink
Merge pull request #119 from php-http/patch-exception
Browse files Browse the repository at this point in the history
Better exception message when we do not find candidates
  • Loading branch information
dbu committed Dec 29, 2018
2 parents d0b9038 + 4f88dba commit a6beda1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ClassDiscovery.php
Expand Up @@ -4,6 +4,7 @@

use Http\Discovery\Exception\ClassInstantiationFailedException;
use Http\Discovery\Exception\DiscoveryFailedException;
use Http\Discovery\Exception\NoCandidateFoundException;
use Http\Discovery\Exception\StrategyUnavailableException;

/**
Expand Down Expand Up @@ -70,6 +71,8 @@ protected static function findOneByType($type)

return $candidate['class'];
}

$exceptions[] = new NoCandidateFoundException($strategy, $candidates);
}

throw DiscoveryFailedException::create($exceptions);
Expand Down
37 changes: 37 additions & 0 deletions src/Exception/NoCandidateFoundException.php
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Http\Discovery\Exception;

use Http\Discovery\Exception;

/**
* When we have used a strategy but no candidates provided by that strategy could be used.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class NoCandidateFoundException extends \Exception implements Exception
{
/**
* @param string $strategy
* @param array $candidates
*/
public function __construct($strategy, array $candidates)
{
$classes = array_map(
function ($a) {
return $a['class'];
},
$candidates
);

$message = sprintf(
'No valid candidate found using strategy "%s". We tested the following candidates: %s.',
$strategy,
implode(', ', $classes)
);

parent::__construct($message);
}
}

0 comments on commit a6beda1

Please sign in to comment.