Skip to content

Commit a24bd6e

Browse files
committed
Allow return a Promise in onRejected then
1 parent 191a0a1 commit a24bd6e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spec/Promise/HttpRejectedPromiseSpec.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Http\Client\Exception;
66
use Http\Client\Exception\TransferException;
7+
use Http\Client\Promise\HttpFulfilledPromise;
78
use Http\Promise\Promise;
89
use PhpSpec\ObjectBehavior;
910
use Prophecy\Argument;
@@ -87,4 +88,19 @@ function it_throws_not_http_exception()
8788
throw new \Exception();
8889
});
8990
}
91+
92+
function it_returns_a_promise_when_rejected(ResponseInterface $response)
93+
{
94+
$exception = new TransferException();
95+
$this->beConstructedWith($exception);
96+
97+
$promise = $this->then(null, function (Exception $exceptionReceived) use($exception, $response) {
98+
return new HttpFulfilledPromise($response->getWrappedObject());
99+
});
100+
101+
$promise->shouldHaveType('Http\Promise\Promise');
102+
$promise->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise');
103+
$promise->getState()->shouldReturn(Promise::FULFILLED);
104+
$promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
105+
}
90106
}

src/Promise/HttpRejectedPromise.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Http\Client\Exception;
66
use Http\Promise\Promise;
7+
use Psr\Http\Message\ResponseInterface;
78

89
final class HttpRejectedPromise implements Promise
910
{
@@ -27,7 +28,11 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
2728
}
2829

2930
try {
30-
return new HttpFulfilledPromise($onRejected($this->exception));
31+
$result = $onRejected($this->exception);
32+
if ($result instanceof Promise) {
33+
return $result;
34+
}
35+
return new HttpFulfilledPromise($result);
3136
} catch (Exception $e) {
3237
return new self($e);
3338
}

0 commit comments

Comments
 (0)