Skip to content

Commit

Permalink
[SNAPI-10272, SNAPI-11508] Add error handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
mykhailo-k committed May 11, 2022
1 parent c54a378 commit e6d2986
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
30 changes: 30 additions & 0 deletions src/Service/Request/Handler/DefaultErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types = 1);

namespace SignNow\Rest\Service\Request\Handler;

use Exception;
use GuzzleHttp\Exception\RequestException;
use SignNow\Rest\Entity\Error;

/**
* Class DefaultErrorHandler
*
* @package SignNow\Rest\Service\Request\Handler
*/
class DefaultErrorHandler implements ErrorHandlerInterface
{
/**
* @param Exception $exception
*
* @return Error
*/
public function handle(Exception $exception): Error
{
$error = new Error();
$error->setResponse(($exception instanceof RequestException) ? $exception->getResponse() : null);
$error->setMessage($exception->getMessage());

return $error;
}
}
22 changes: 22 additions & 0 deletions src/Service/Request/Handler/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types = 1);

namespace SignNow\Rest\Service\Request\Handler;

use Exception;
use SignNow\Rest\Entity\Error;

/**
* Interface ErrorHandlerInterface
*
* @package SignNow\Rest\Service\Request\Handler
*/
interface ErrorHandlerInterface
{
/**
* @param Exception $exception
*
* @return Error
*/
public function handle(Exception $exception): Error;
}
51 changes: 35 additions & 16 deletions src/Service/Request/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;
use LogicException;
use SignNow\Rest\Entity\Collection\Errors;
use SignNow\Rest\Entity\Error;
use SignNow\Rest\EntityManager\Exception\PoolException;
use SignNow\Rest\Service\Request\Handler\DefaultErrorHandler;
use SignNow\Rest\Service\Request\Handler\ErrorHandlerInterface;
use SignNow\Rest\Service\Request\Pool\Item;

/**
Expand All @@ -25,6 +25,11 @@ class Pool implements PoolInterface
*/
protected $client;

/**
* @var ErrorHandlerInterface
*/
private $errorHandler;

/**
* @var Item[]
*/
Expand All @@ -39,10 +44,12 @@ class Pool implements PoolInterface
* Pool constructor.
*
* @param ClientInterface $client
* @param ErrorHandlerInterface|null $errorHandler
*/
public function __construct(ClientInterface $client)
public function __construct(ClientInterface $client, ?ErrorHandlerInterface $errorHandler = null)
{
$this->client = $client;
$this->errorHandler = $errorHandler ?: new DefaultErrorHandler();
$this->errors = new Errors();
}

Expand All @@ -63,19 +70,8 @@ public function send(int $concurrencyLimit = null): PoolInterface
}, $this->items);

$options = [
'fulfilled' => function (Response $response, $index) {
$this->items[$index]->setResponse($response);
},
'rejected' => function (Exception $reason, $index, $promise) {
$error = new Error();
$response = ($reason instanceof RequestException) ? $reason->getResponse() : null;

$error->setResponse($response);
$error->setMessage($reason->getMessage());
$this->errors->push((int)$index, $error);

$this->items[$index]->setError($this->errors->getError($index));
},
'fulfilled' => $this->onFulfilled(),
'rejected' => $this->onRejected(),
];

if ($concurrencyLimit) {
Expand Down Expand Up @@ -144,4 +140,27 @@ public function getErrors(): Errors
{
return $this->errors;
}

/**
* @return callable
*/
protected function onFulfilled(): callable
{
return function (Response $response, $index) {
$this->items[$index]->setResponse($response);
};
}

/**
* @return callable
*/
protected function onRejected(): callable
{
return function (Exception $reason, $index, $promise) {
$error = $this->errorHandler->handle($reason);

$this->errors->push((int)$index, $error);
$this->items[$index]->setError($error);
};
}
}

0 comments on commit e6d2986

Please sign in to comment.