diff --git a/src/Service/Request/Handler/DefaultErrorHandler.php b/src/Service/Request/Handler/DefaultErrorHandler.php new file mode 100644 index 0000000..00bde8e --- /dev/null +++ b/src/Service/Request/Handler/DefaultErrorHandler.php @@ -0,0 +1,30 @@ +setResponse(($exception instanceof RequestException) ? $exception->getResponse() : null); + $error->setMessage($exception->getMessage()); + + return $error; + } +} diff --git a/src/Service/Request/Handler/ErrorHandlerInterface.php b/src/Service/Request/Handler/ErrorHandlerInterface.php new file mode 100644 index 0000000..3dec43c --- /dev/null +++ b/src/Service/Request/Handler/ErrorHandlerInterface.php @@ -0,0 +1,22 @@ +client = $client; + $this->errorHandler = $errorHandler ?: new DefaultErrorHandler(); $this->errors = new Errors(); } @@ -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) { @@ -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); + }; + } }