Skip to content

Commit 5a1c84b

Browse files
committed
Catch parsing exceptions.
1 parent ebea4fb commit 5a1c84b

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/CurlHttpClient.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ public function __destruct()
9595
* @return ResponseInterface
9696
*
9797
* @throws \UnexpectedValueException if unsupported HTTP version requested
98-
* @throws \InvalidArgumentException
9998
* @throws RequestException
10099
*
101100
* @since 1.0
@@ -119,7 +118,12 @@ public function sendRequest(RequestInterface $request)
119118

120119
$info = curl_getinfo($this->handle);
121120

122-
return $this->responseParser->parse($raw, $info);
121+
try {
122+
$response = $this->responseParser->parse($raw, $info);
123+
} catch (\Exception $e) {
124+
throw new RequestException($e->getMessage(), $request, $e);
125+
}
126+
return $response;
123127
}
124128

125129
/**

src/MultiRunner.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __destruct()
5454
}
5555

5656
/**
57-
* Add promise core
57+
* Add promise to runner
5858
*
5959
* @param PromiseCore $core
6060
*/
@@ -74,6 +74,22 @@ public function add(PromiseCore $core)
7474
curl_multi_add_handle($this->multiHandle, $core->getHandle());
7575
}
7676

77+
/**
78+
* Remove promise from runner
79+
*
80+
* @param PromiseCore $core
81+
*/
82+
public function remove(PromiseCore $core)
83+
{
84+
foreach ($this->cores as $index => $existed) {
85+
if ($existed === $core) {
86+
curl_multi_remove_handle($this->multiHandle, $core->getHandle());
87+
unset($this->cores[$index]);
88+
return;
89+
}
90+
}
91+
}
92+
7793
/**
7894
* Wait for request(s) to be completed.
7995
*
@@ -86,18 +102,32 @@ public function wait(PromiseCore $targetCore = null)
86102
$info = curl_multi_info_read($this->multiHandle);
87103
if (false !== $info) {
88104
$core = $this->findCoreByHandle($info['handle']);
105+
106+
if (null === $core) {
107+
// We have no promise for this handle. Drop it.
108+
curl_multi_remove_handle($this->multiHandle, $info['handle']);
109+
continue;
110+
}
111+
89112
if (CURLE_OK === $info['result']) {
90-
$response = $this->responseParser->parse(
91-
curl_multi_getcontent($info['handle']),
92-
curl_getinfo($info['handle'])
93-
);
94-
$core->fulfill($response);
113+
try {
114+
$response = $this->responseParser->parse(
115+
curl_multi_getcontent($core->getHandle()),
116+
curl_getinfo($core->getHandle())
117+
);
118+
$core->fulfill($response);
119+
} catch (\Exception $e) {
120+
$core->reject(
121+
new RequestException($e->getMessage(), $core->getRequest(), $e)
122+
);
123+
}
95124
} else {
96-
$error = curl_error($info['handle']);
97-
$exception = new RequestException($error, $core->getRequest());
98-
$core->reject($exception);
125+
$error = curl_error($core->getHandle());
126+
$core->reject(new RequestException($error, $core->getRequest()));
99127
}
128+
$this->remove($core);
100129

130+
// This is a promise we are waited for. So exiting wait().
101131
if ($core === $targetCore) {
102132
return;
103133
}

0 commit comments

Comments
 (0)