Skip to content

Commit

Permalink
feat: pass service name to RejectedException
Browse files Browse the repository at this point in the history
  • Loading branch information
marmichalski committed Jan 13, 2025
1 parent d171924 commit ded6c90
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/Ganesha/Exception/RejectedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,22 @@

class RejectedException extends \RuntimeException
{
public function __construct(
string $message = "",
int $code = 0,
?\Throwable $previous = null,
private ?string $serviceName = null,
) {
parent::__construct($message, $code, $previous);
}

public static function withServiceName(string $serviceName): self
{
return new self(sprintf('"%s" is not available', $serviceName), serviceName: $serviceName);
}

public function serviceName(): ?string
{
return $this->serviceName;
}
}
2 changes: 1 addition & 1 deletion src/Ganesha/GaneshaHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function request(string $method, string $url, array $options = []): Respo
$serviceName = $this->serviceNameExtractor->extract($method, $url, $options);

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

$response = $this->client->request($method, $url, $this->avoidGaneshaOptionsPropagation($options));
Expand Down
4 changes: 1 addition & 3 deletions src/Ganesha/GuzzleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public function __invoke(callable $handler): \Closure
if (!$this->ganesha->isAvailable($serviceName)) {
return call_user_func(
$this->rejectionForFunction,
new RejectedException(
sprintf('"%s" is not available', $serviceName)
)
RejectedException::withServiceName($serviceName),
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Ackintosh/Ganesha/GaneshaHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function reject(): void
$ganesha->failure($service);
$ganesha->failure($service);

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

Expand Down
4 changes: 2 additions & 2 deletions tests/Ackintosh/Ganesha/GuzzleMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ public function recordsFailureOnRequestTimedOut()
*/
public function reject()
{
$this->expectException(RejectedException::class);

// Build Ganesha which has count strategy with memcached adapter
$m = new \Memcached();
$m->addServer(
Expand All @@ -180,6 +178,8 @@ public function reject()
$ganesha->failure($service);
$ganesha->failure($service);

$this->expectExceptionObject(RejectedException::withServiceName($service));

$client->get('http://' . $service . '/awesome_resource');
}

Expand Down

0 comments on commit ded6c90

Please sign in to comment.