diff --git a/CHANGELOG.md b/CHANGELOG.md index 29b03e0..d59bec9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [2.3.0] - 2022-02-x + +### Changed + +- Enabled the `$onRejected` callback of `HttpRejectedPromise` to return a promise for implementing a retry + mechanism [#168](https://github.com/php-http/httplug/pull/168) + ## [2.2.0] - 2020-07-13 ### Changed diff --git a/spec/Promise/HttpRejectedPromiseSpec.php b/spec/Promise/HttpRejectedPromiseSpec.php index f46dcc5..5bc3f82 100644 --- a/spec/Promise/HttpRejectedPromiseSpec.php +++ b/spec/Promise/HttpRejectedPromiseSpec.php @@ -4,6 +4,7 @@ use Http\Client\Exception; use Http\Client\Exception\TransferException; +use Http\Client\Promise\HttpFulfilledPromise; use Http\Promise\Promise; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -87,4 +88,19 @@ function it_throws_not_http_exception() throw new \Exception(); }); } + + function it_returns_a_promise_when_rejected(ResponseInterface $response) + { + $exception = new TransferException(); + $this->beConstructedWith($exception); + + $promise = $this->then(null, function (Exception $exceptionReceived) use($exception, $response) { + return new HttpFulfilledPromise($response->getWrappedObject()); + }); + + $promise->shouldHaveType('Http\Promise\Promise'); + $promise->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise'); + $promise->getState()->shouldReturn(Promise::FULFILLED); + $promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); + } } diff --git a/src/Promise/HttpRejectedPromise.php b/src/Promise/HttpRejectedPromise.php index 8af97de..624cc8a 100644 --- a/src/Promise/HttpRejectedPromise.php +++ b/src/Promise/HttpRejectedPromise.php @@ -27,7 +27,12 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) } try { - return new HttpFulfilledPromise($onRejected($this->exception)); + $result = $onRejected($this->exception); + if ($result instanceof Promise) { + return $result; + } + + return new HttpFulfilledPromise($result); } catch (Exception $e) { return new self($e); }