Skip to content
Snippets Groups Projects
Unverified Commit 16e8b6f8 authored by Enrico Zimuel's avatar Enrico Zimuel Committed by GitHub
Browse files

Allow returning a Promise in onRejected then() (#168)

parent fc6a1399
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -4,6 +4,7 @@ namespace spec\Http\Client\Promise;
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 @@ class HttpRejectedPromiseSpec extends ObjectBehavior
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');
}
}
......@@ -27,7 +27,12 @@ final class HttpRejectedPromise implements Promise
}
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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment