diff --git a/README.md b/README.md index ef7236a..0812321 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,9 @@ var_dump( ); ``` +If there is an issues with the request, such as misconfigured CURL SSL options, an `InvalidRequest` will be thrown +with message from CURL on why the request failed. Use the message as a hit to troubleshooting steps of your environment. + # Usage diff --git a/lib/Client.php b/lib/Client.php index ec732b9..35cad81 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -13,6 +13,8 @@ namespace SendGrid; +use SendGrid\Exception\InvalidRequest; + /** * * Class Client @@ -453,6 +455,7 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea * @param bool $retryOnLimit should retry if rate limit is reach? * * @return Response object + * @throws InvalidRequest */ public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = false) { @@ -463,6 +466,10 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry curl_setopt_array($channel, $options); $content = curl_exec($channel); + if ($content === false) { + throw new InvalidRequest(curl_error($channel), curl_errno($channel)); + } + $response = $this->parseResponse($channel, $content); if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $retryOnLimit) { diff --git a/lib/Exception/InvalidRequest.php b/lib/Exception/InvalidRequest.php new file mode 100644 index 0000000..bbf8406 --- /dev/null +++ b/lib/Exception/InvalidRequest.php @@ -0,0 +1,36 @@ + + * @author Elmer Thomas + * @copyright 2018 SendGrid + * @license https://opensource.org/licenses/MIT The MIT License + * @version GIT: + * @link http://packagist.org/packages/sendgrid/php-http-client + */ +namespace SendGrid\Exception; + +use Throwable; + +/** + * Class InvalidHttpRequest + * + * Thrown when invalid payload was constructed, which could not reach SendGrid server. + * + * @package SendGrid\Exceptions + */ +class InvalidRequest extends \Exception +{ + public function __construct( + $message = "", + $code = 0, + Throwable $previous = null + ) { + $message = 'Could not send request to server. '. + 'CURL error '.$code.': '.$message; + parent::__construct($message, $code, $previous); + } + +} diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index 538b1b0..4f506ec 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -3,6 +3,7 @@ namespace SendGrid\Test; use SendGrid\Client; +use SendGrid\Exception\InvalidRequest; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -195,6 +196,14 @@ public function testCreateCurlOptionsWithBodyAndHeaders() ], $result); } + + public function testThrowExceptionOnInvalidCall() + { + $this->setExpectedException(InvalidRequest::class); + $client = new Client('invalid://url',['User-Agent: Custom-Client 1.0']); + $client->get(); + } + /** * @param object $obj * @param string $name