Skip to content

Commit

Permalink
Add support for different errors on curl functions (curl_, curl_multi…
Browse files Browse the repository at this point in the history
…_ and curl_share_)
  • Loading branch information
jfoulquie-tnw committed Mar 30, 2022
1 parent 940be03 commit 7c119d5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
21 changes: 17 additions & 4 deletions generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,29 @@ private function generateExceptionCode(string $moduleName, Method $method) : str
default:
throw new \LogicException("Method doesn't have an error type");
}

// Special case for CURL: we need the first argument of the method if this is a resource.
if ($moduleName === 'Curl') {
$params = $method->getParams();
if (\count($params) > 0 && $params[0]->getParameter() === 'ch') {
return "
if (\count($params) > 0) {
if ($params[0]->getParameter() === 'handle') {
return "
if (\$result === $errorValue) {
throw CurlException::createFromCurlHandle(\$handle);
}
";
} elseif ($params[0]->getParameter() === 'multi_handle') {
return "
if (\$result === $errorValue) {
throw CurlException::createFromCurlResource(\$ch);
throw CurlException::createFromCurlMultiHandle(\$multi_handle);
}
";
} elseif ($params[0]->getParameter() === 'share_handle') {
return "
if (\$result === $errorValue) {
throw CurlException::createFromCurlShareHandle(\$share_handle);
}
";
}
}
}

Expand Down
32 changes: 29 additions & 3 deletions lib/Exceptions/CurlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,39 @@

namespace Safe\Exceptions;

use CurlHandle;
use CurlMultiHandle;
use CurlShareHandle;

class CurlException extends \Exception implements SafeExceptionInterface
{
/**
* @param \CurlHandle $ch
*
* @param array<string, string> $error
* @return \Safe\Exceptions\CurlException
*/
public static function createFromPhpError(array $error = []): self
{
return new self(
$error['message'] ?? 'An error occured',
);
}

/**
* @param \CurlHandle $handle
*/
public static function createFromPhpError($ch): self
public static function createFromCurlHandle(CurlHandle $handle): self
{
return new self(\curl_error($handle), \curl_errno($handle));
}

public static function createFromCurlMultiHandle(CurlMultiHandle $multiHandle) : self
{
return new self(\curl_multi_strerror(\curl_multi_errno($multiHandle)) ?? '', \curl_multi_errno($multiHandle));
}

public static function createFromCurlShareHandle(CurlShareHandle $shareHandle) : self
{
return new self(\curl_error($ch), \curl_errno($ch));
return new self(\curl_share_strerror(\curl_share_errno($shareHandle)) ?? '', \curl_share_errno($shareHandle));
}
}

0 comments on commit 7c119d5

Please sign in to comment.