Skip to content

Commit b491dcf

Browse files
authored
Merge pull request #121 from marmichalski/rejected-ex-srv
feat: pass service name to RejectedException
2 parents 86ceb61 + 918ff78 commit b491dcf

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

src/Ganesha/Exception/RejectedException.php

+18
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,22 @@
44

55
class RejectedException extends \RuntimeException
66
{
7+
public function __construct(
8+
string $message = "",
9+
int $code = 0,
10+
?\Throwable $previous = null,
11+
private ?string $serviceName = null,
12+
) {
13+
parent::__construct($message, $code, $previous);
14+
}
15+
16+
public static function withServiceName(string $serviceName): self
17+
{
18+
return new self(sprintf('"%s" is not available', $serviceName), serviceName: $serviceName);
19+
}
20+
21+
public function serviceName(): ?string
22+
{
23+
return $this->serviceName;
24+
}
725
}

src/Ganesha/GaneshaHttpClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function request(string $method, string $url, array $options = []): Respo
5959
$serviceName = $this->serviceNameExtractor->extract($method, $url, $options);
6060

6161
if (!$this->ganesha->isAvailable($serviceName)) {
62-
throw new RejectedException(sprintf('"%s" is not available', $serviceName));
62+
throw RejectedException::withServiceName($serviceName);
6363
}
6464

6565
$response = $this->client->request($method, $url, $this->avoidGaneshaOptionsPropagation($options));

src/Ganesha/GuzzleMiddleware.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ public function __invoke(callable $handler): \Closure
6969
if (!$this->ganesha->isAvailable($serviceName)) {
7070
return call_user_func(
7171
$this->rejectionForFunction,
72-
new RejectedException(
73-
sprintf('"%s" is not available', $serviceName)
74-
)
72+
RejectedException::withServiceName($serviceName),
7573
);
7674
}
7775

tests/Ackintosh/Ganesha/GaneshaHttpClientTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function reject(): void
239239
$ganesha->failure($service);
240240
$ganesha->failure($service);
241241

242-
$this->expectException(RejectedException::class);
242+
$this->expectExceptionObject(RejectedException::withServiceName($service));
243243
$client->request('GET', 'http://'.$service.'/awesome_resource');
244244
}
245245

tests/Ackintosh/Ganesha/GuzzleMiddlewareTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ public function recordsFailureOnRequestTimedOut()
155155
*/
156156
public function reject()
157157
{
158-
$this->expectException(RejectedException::class);
159-
160158
// Build Ganesha which has count strategy with memcached adapter
161159
$m = new \Memcached();
162160
$m->addServer(
@@ -180,6 +178,8 @@ public function reject()
180178
$ganesha->failure($service);
181179
$ganesha->failure($service);
182180

181+
$this->expectExceptionObject(RejectedException::withServiceName($service));
182+
183183
$client->get('http://' . $service . '/awesome_resource');
184184
}
185185

0 commit comments

Comments
 (0)