Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow returning a Promise in onRejected then() #168

Merged
merged 5 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `HttpRejectedPromise` to return a promise for implementing a retry
dbu marked this conversation as resolved.
Show resolved Hide resolved
mechanism [#168](https://github.com/php-http/httplug/pull/168)

## [2.2.0] - 2020-07-13

### Changed
Expand Down
16 changes: 16 additions & 0 deletions spec/Promise/HttpRejectedPromiseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}
6 changes: 5 additions & 1 deletion src/Promise/HttpRejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ 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);
dbu marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception $e) {
return new self($e);
}
Expand Down