Skip to content

Commit e36e168

Browse files
authored
feat: Throw an InvalidRequest whenever a curl request fails (#103)
1 parent 8653c40 commit e36e168

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

TROUBLESHOOTING.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ If you can't find a solution below, please open an [issue](https://github.com/se
33
## Table of Contents
44

55
* [Viewing the Request Body](#request-body)
6+
* [Handling SSL Errors](#ssl-errors)
67

78
<a name="request-body"></a>
89
## Viewing the Request Body
@@ -14,3 +15,10 @@ echo $response->statusCode();
1415
echo $response->body();
1516
echo $response->headers();
1617
```
18+
19+
<a name="ssl-errors">
20+
## Handling SSL Errors
21+
22+
If any SSL errors occur during API calls, an `InvalidRequest` will be thrown. This will provide information to help debug the issue further.
23+
24+
If the issue is caused by an unrecognized certificate, it may be possible that PHP is unable to locate your system's CA bundle. An easy fix would be requiring the `composer/ca-bundle` package - this library will automatically detect and use that to locate the CA bundle, or use Mozilla's as a fallback.

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"squizlabs/php_codesniffer": "~2.0",
2828
"friendsofphp/php-cs-fixer": "^2.16"
2929
},
30+
"suggest": {
31+
"composer/ca-bundle": "Including this library will ensure that a valid CA bundle is available for secure connections"
32+
},
3033
"autoload": {
3134
"psr-4": {
3235
"SendGrid\\": "lib/"

lib/Client.php

+16
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,15 @@ private function createCurlOptions($method, $body = null, $headers = null)
369369
}
370370
$options[CURLOPT_HTTPHEADER] = $headers;
371371

372+
if (class_exists('\\Composer\\CaBundle\\CaBundle') && method_exists('\\Composer\\CaBundle\\CaBundle', 'getSystemCaRootBundlePath')) {
373+
$caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();
374+
if (is_dir($caPathOrFile) || (is_link($caPathOrFile) && is_dir(readlink($caPathOrFile)))) {
375+
$options[CURLOPT_CAPATH] = $caPathOrFile;
376+
} else {
377+
$options[CURLOPT_CAINFO] = $caPathOrFile;
378+
}
379+
}
380+
372381
return $options;
373382
}
374383

@@ -492,6 +501,8 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
492501
* @param array $requests
493502
*
494503
* @return Response[]
504+
*
505+
* @throws InvalidRequest
495506
*/
496507
public function makeAllRequests(array $requests = [])
497508
{
@@ -512,6 +523,11 @@ public function makeAllRequests(array $requests = [])
512523
$sleepDurations = 0;
513524
foreach ($channels as $id => $channel) {
514525
$content = curl_multi_getcontent($channel);
526+
527+
if ($content === false) {
528+
throw new InvalidRequest(curl_error($channel), curl_errno($channel));
529+
}
530+
515531
$response = $this->parseResponse($channel, $content);
516532

517533
if ($requests[$id]['retryOnLimit'] && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) {

test/unit/ClientTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testConstructor()
3333
$this->assertAttributeEquals([], 'path', $this->client);
3434
$this->assertAttributeEquals([], 'curlOptions', $this->client);
3535
$this->assertAttributeEquals(false, 'retryOnLimit', $this->client);
36-
$this->assertAttributeEquals(['get', 'post', 'patch', 'put', 'delete'], 'methods', $this->client);
36+
$this->assertAttributeEquals(['get', 'post', 'patch', 'put', 'delete'], 'methods', $this->client);
3737
}
3838

3939
public function test_()
@@ -209,6 +209,15 @@ public function testThrowExceptionOnInvalidCall()
209209
$client->get();
210210
}
211211

212+
public function testMakeRequestWithUntrustedRootCert()
213+
{
214+
$this->expectException(InvalidRequest::class);
215+
$this->expectExceptionMessageRegExp('/certificate/i');
216+
217+
$client = new Client('https://untrusted-root.badssl.com/');
218+
$client->makeRequest('GET', 'https://untrusted-root.badssl.com/');
219+
}
220+
212221
/**
213222
* @param object $obj
214223
* @param string $name

0 commit comments

Comments
 (0)