Skip to content

Commit 4a588d1

Browse files
committed
Fix #26: Combining CurlClient with StopwatchPlugin causes Promise onRejected handler to never be invoked.
1 parent fc7209f commit 4a588d1

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/PromiseCore.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ public function fulfill()
185185
$this->state = Promise::FULFILLED;
186186
$response = $this->responseBuilder->getResponse();
187187
$response->getBody()->seek(0);
188-
$response = $this->call($this->onFulfilled, $response);
188+
189+
while (count($this->onFulfilled) > 0) {
190+
$callback = array_shift($this->onFulfilled);
191+
$response = call_user_func($callback, $response);
192+
}
193+
189194
if ($response instanceof ResponseInterface) {
190195
$this->responseBuilder->setResponse($response);
191196
}
@@ -201,10 +206,14 @@ public function reject(Exception $exception)
201206
$this->exception = $exception;
202207
$this->state = Promise::REJECTED;
203208

204-
try {
205-
$this->call($this->onRejected, $this->exception);
206-
} catch (Exception $exception) {
207-
$this->exception = $exception;
209+
while (count($this->onRejected) > 0) {
210+
$callback = array_shift($this->onRejected);
211+
try {
212+
$exception = call_user_func($callback, $this->exception);
213+
$this->exception = $exception;
214+
} catch (Exception $exception) {
215+
$this->exception = $exception;
216+
}
208217
}
209218
}
210219

tests/PromiseCoreTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,39 @@ function (RequestException $exception) {
8383
static::assertEquals('Bar', $core->getException()->getMessage());
8484
}
8585

86+
/**
87+
* «onReject» callback can throw exception.
88+
*
89+
* @see https://github.com/php-http/curl-client/issues/26
90+
*/
91+
public function testIssue26()
92+
{
93+
$request = $this->createRequest('GET', '/');
94+
$this->handle = curl_init();
95+
96+
$core = new PromiseCore(
97+
$request,
98+
$this->handle,
99+
new ResponseBuilder($this->createResponse())
100+
);
101+
$core->addOnRejected(
102+
function (RequestException $exception) {
103+
throw new RequestException('Foo', $exception->getRequest(), $exception);
104+
}
105+
);
106+
$core->addOnRejected(
107+
function (RequestException $exception) {
108+
return new RequestException('Bar', $exception->getRequest(), $exception);
109+
}
110+
);
111+
112+
$exception = new RequestException('Error', $request);
113+
$core->reject($exception);
114+
static::assertEquals(Promise::REJECTED, $core->getState());
115+
static::assertInstanceOf(Exception::class, $core->getException());
116+
static::assertEquals('Bar', $core->getException()->getMessage());
117+
}
118+
86119
/**
87120
* @expectedException \LogicException
88121
*/

0 commit comments

Comments
 (0)