Skip to content

Better exception message when we do not find candidates #119

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 1 commit into from
Dec 29, 2018
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
3 changes: 3 additions & 0 deletions src/ClassDiscovery.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
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);
}
}