Skip to content

Commit 34ba05a

Browse files
committed
Add support for different errors on curl functions (curl_, curl_multi_ and curl_share_)
1 parent 89e9046 commit 34ba05a

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

generator/src/WritePhpFunction.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,29 @@ private function generateExceptionCode(string $moduleName, Method $method) : str
132132
default:
133133
throw new \LogicException("Method doesn't have an error type");
134134
}
135-
136135
// Special case for CURL: we need the first argument of the method if this is a resource.
137136
if ($moduleName === 'Curl') {
138137
$params = $method->getParams();
139-
if (\count($params) > 0 && $params[0]->getParameter() === 'ch') {
140-
return "
138+
if (\count($params) > 0) {
139+
if ($params[0]->getParameter() === 'handle') {
140+
return "
141+
if (\$result === $errorValue) {
142+
throw CurlException::createFromCurlHandle(\$handle);
143+
}
144+
";
145+
} elseif ($params[0]->getParameter() === 'multi_handle') {
146+
return "
141147
if (\$result === $errorValue) {
142-
throw CurlException::createFromCurlResource(\$ch);
148+
throw CurlException::createFromCurlMultiHandle(\$multi_handle);
143149
}
144150
";
151+
} elseif ($params[0]->getParameter() === 'share_handle') {
152+
return "
153+
if (\$result === $errorValue) {
154+
throw CurlException::createFromCurlShareHandle(\$share_handle);
155+
}
156+
";
157+
}
145158
}
146159
}
147160

lib/Exceptions/CurlException.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,39 @@
33

44
namespace Safe\Exceptions;
55

6+
use CurlHandle;
7+
use CurlMultiHandle;
8+
use CurlShareHandle;
9+
610
class CurlException extends \Exception implements SafeExceptionInterface
711
{
812
/**
9-
* @param \CurlHandle $ch
13+
*
14+
* @param array<string, string> $error
15+
* @return \Safe\Exceptions\CurlException
16+
*/
17+
public static function createFromPhpError(array $error = []): self
18+
{
19+
return new self(
20+
$error['message'] ?? 'An error occured',
21+
);
22+
}
23+
24+
/**
25+
* @param \CurlHandle $handle
1026
*/
11-
public static function createFromPhpError($ch): self
27+
public static function createFromCurlHandle(CurlHandle $handle): self
28+
{
29+
return new self(\curl_error($handle), \curl_errno($handle));
30+
}
31+
32+
public static function createFromCurlMultiHandle(CurlMultiHandle $multiHandle) : self
33+
{
34+
return new self(\curl_multi_strerror(\curl_multi_errno($multiHandle)) ?? '', \curl_multi_errno($multiHandle));
35+
}
36+
37+
public static function createFromCurlShareHandle(CurlShareHandle $shareHandle) : self
1238
{
13-
return new self(\curl_error($ch), \curl_errno($ch));
39+
return new self(\curl_share_strerror(\curl_share_errno($shareHandle)) ?? '', \curl_share_errno($shareHandle));
1440
}
1541
}

0 commit comments

Comments
 (0)