Skip to content

Commit db6d34e

Browse files
Merge pull request #99 from alextech/hotfix/exception_on_invalid_request
Throw InvalidRequest exception on invalid CURL request
2 parents 4e7dc81 + 9d6ea4d commit db6d34e

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ var_dump(
147147
);
148148
```
149149

150+
If there is an issues with the request, such as misconfigured CURL SSL options, an `InvalidRequest` will be thrown
151+
with message from CURL on why the request failed. Use the message as a hit to troubleshooting steps of your environment.
152+
150153
<a name="usage"></a>
151154
# Usage
152155

lib/Client.php

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace SendGrid;
1515

16+
use SendGrid\Exception\InvalidRequest;
17+
1618
/**
1719
*
1820
* Class Client
@@ -453,6 +455,7 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea
453455
* @param bool $retryOnLimit should retry if rate limit is reach?
454456
*
455457
* @return Response object
458+
* @throws InvalidRequest
456459
*/
457460
public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = false)
458461
{
@@ -463,6 +466,10 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
463466
curl_setopt_array($channel, $options);
464467
$content = curl_exec($channel);
465468

469+
if ($content === false) {
470+
throw new InvalidRequest(curl_error($channel), curl_errno($channel));
471+
}
472+
466473
$response = $this->parseResponse($channel, $content);
467474

468475
if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $retryOnLimit) {

lib/Exception/InvalidRequest.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* HTTP Client library
5+
*
6+
* @author Matt Bernier <[email protected]>
7+
* @author Elmer Thomas <[email protected]>
8+
* @copyright 2018 SendGrid
9+
* @license https://opensource.org/licenses/MIT The MIT License
10+
* @version GIT: <git_id>
11+
* @link http://packagist.org/packages/sendgrid/php-http-client
12+
*/
13+
namespace SendGrid\Exception;
14+
15+
use Throwable;
16+
17+
/**
18+
* Class InvalidHttpRequest
19+
*
20+
* Thrown when invalid payload was constructed, which could not reach SendGrid server.
21+
*
22+
* @package SendGrid\Exceptions
23+
*/
24+
class InvalidRequest extends \Exception
25+
{
26+
public function __construct(
27+
$message = "",
28+
$code = 0,
29+
Throwable $previous = null
30+
) {
31+
$message = 'Could not send request to server. '.
32+
'CURL error '.$code.': '.$message;
33+
parent::__construct($message, $code, $previous);
34+
}
35+
36+
}

test/unit/ClientTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SendGrid\Test;
44

55
use SendGrid\Client;
6+
use SendGrid\Exception\InvalidRequest;
67

78
class ClientTest extends \PHPUnit_Framework_TestCase
89
{
@@ -195,6 +196,14 @@ public function testCreateCurlOptionsWithBodyAndHeaders()
195196
], $result);
196197
}
197198

199+
200+
public function testThrowExceptionOnInvalidCall()
201+
{
202+
$this->setExpectedException(InvalidRequest::class);
203+
$client = new Client('invalid://url',['User-Agent: Custom-Client 1.0']);
204+
$client->get();
205+
}
206+
198207
/**
199208
* @param object $obj
200209
* @param string $name

0 commit comments

Comments
 (0)