Skip to content

Commit a6beda1

Browse files
authored
Merge pull request #119 from php-http/patch-exception
Better exception message when we do not find candidates
2 parents d0b9038 + 4f88dba commit a6beda1

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/ClassDiscovery.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Http\Discovery\Exception\ClassInstantiationFailedException;
66
use Http\Discovery\Exception\DiscoveryFailedException;
7+
use Http\Discovery\Exception\NoCandidateFoundException;
78
use Http\Discovery\Exception\StrategyUnavailableException;
89

910
/**
@@ -70,6 +71,8 @@ protected static function findOneByType($type)
7071

7172
return $candidate['class'];
7273
}
74+
75+
$exceptions[] = new NoCandidateFoundException($strategy, $candidates);
7376
}
7477

7578
throw DiscoveryFailedException::create($exceptions);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Http\Discovery\Exception;
6+
7+
use Http\Discovery\Exception;
8+
9+
/**
10+
* When we have used a strategy but no candidates provided by that strategy could be used.
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
final class NoCandidateFoundException extends \Exception implements Exception
15+
{
16+
/**
17+
* @param string $strategy
18+
* @param array $candidates
19+
*/
20+
public function __construct($strategy, array $candidates)
21+
{
22+
$classes = array_map(
23+
function ($a) {
24+
return $a['class'];
25+
},
26+
$candidates
27+
);
28+
29+
$message = sprintf(
30+
'No valid candidate found using strategy "%s". We tested the following candidates: %s.',
31+
$strategy,
32+
implode(', ', $classes)
33+
);
34+
35+
parent::__construct($message);
36+
}
37+
}

0 commit comments

Comments
 (0)