Skip to content
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

feat: pass service name to RejectedException #121

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
feat: pass service name to RejectedException
  • Loading branch information
marmichalski committed Jan 13, 2025
commit ded6c90629001c11a7d07bac474ed1b8940eea4f
18 changes: 18 additions & 0 deletions src/Ganesha/Exception/RejectedException.php
Original file line number Diff line number Diff line change
@@ -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
@@ -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));
4 changes: 1 addition & 3 deletions src/Ganesha/GuzzleMiddleware.php
Original file line number Diff line number Diff line change
@@ -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),
);
}

2 changes: 1 addition & 1 deletion tests/Ackintosh/Ganesha/GaneshaHttpClientTest.php
Original file line number Diff line number Diff line change
@@ -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');
}

4 changes: 2 additions & 2 deletions tests/Ackintosh/Ganesha/GuzzleMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -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(
@@ -180,6 +178,8 @@ public function reject()
$ganesha->failure($service);
$ganesha->failure($service);

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

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

Loading