Skip to content

Commit

Permalink
fix: Exception type mismatch in rejected callback (#410)
Browse files Browse the repository at this point in the history
Fixes #409
  • Loading branch information
sleptor committed Jul 30, 2024
1 parent 469b4d9 commit 761adf3
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/WebPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Base64Url\Base64Url;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class WebPush
Expand Down Expand Up @@ -152,17 +154,11 @@ public function flush(?int $batchSize = null): \Generator
foreach ($requests as $request) {
$promises[] = $this->client->sendAsync($request)
->then(function ($response) use ($request) {
/** @var ResponseInterface $response * */
/** @var ResponseInterface $response **/
return new MessageSentReport($request, $response);
})
->otherwise(function ($reason) {
/** @var RequestException $reason **/
if (method_exists($reason, 'getResponse')) {
$response = $reason->getResponse();
} else {
$response = null;
}
return new MessageSentReport($reason->getRequest(), $response, false, $reason->getMessage());
return $this->createRejectedReport($reason);
});
}

Expand Down Expand Up @@ -205,17 +201,12 @@ public function flushPooled($callback, ?int $batchSize = null, ?int $requestConc
$pool = new Pool($this->client, $batch, [
'requestConcurrency' => $requestConcurrency,
'fulfilled' => function (ResponseInterface $response, int $index) use ($callback, $batch) {
/** @var \Psr\Http\Message\RequestInterface $request **/
/** @var RequestInterface $request **/
$request = $batch[$index];
$callback(new MessageSentReport($request, $response));
},
'rejected' => function (RequestException $reason) use ($callback) {
if (method_exists($reason, 'getResponse')) {
$response = $reason->getResponse();
} else {
$response = null;
}
$callback(new MessageSentReport($reason->getRequest(), $response, false, $reason->getMessage()));
'rejected' => function ($reason) use ($callback) {
$callback($this->createRejectedReport($reason));
},
]);

Expand All @@ -228,6 +219,21 @@ public function flushPooled($callback, ?int $batchSize = null, ?int $requestConc
}
}

/**
* @param RequestException|ConnectException $reason
* @return MessageSentReport
*/
protected function createRejectedReport($reason): MessageSentReport
{
if ($reason instanceof RequestException) {
$response = $reason->getResponse();
} else {
$response = null;
}

return new MessageSentReport($reason->getRequest(), $response, false, $reason->getMessage());
}

/**
* @throws \ErrorException|\Random\RandomException
*/
Expand Down

0 comments on commit 761adf3

Please sign in to comment.