Skip to content

Commit

Permalink
Trigger an E_USER_ERROR whenever a curl request fails (sendgrid#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Oct 2, 2018
1 parent e9a04d9 commit 2c4d52b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ private function parseResponse($channel, $content)
$headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
$statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE);

if ($statusCode === 0) {
trigger_error(curl_error($channel), E_USER_ERROR);
}

$responseBody = substr($content, $headerSize);

$responseHeaders = substr($content, 0, $headerSize);
Expand Down
28 changes: 28 additions & 0 deletions test/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase
private $host;
/** @var array */
private $headers;
/** @var array */
private $errors;

protected function setUp()
{
Expand All @@ -21,6 +23,24 @@ protected function setUp()
'Authorization: Bearer SG.XXXX'
];
$this->client = new MockClient($this->host, $this->headers, '/v3');
$this->errors = [];
set_error_handler([$this, 'errorHandler']);
}

public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
$this->errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
}

public function assertError($expectedErrorStringRegex, $expectedErrorNumber)
{
foreach ($this->errors as $error) {
if ($error['errno'] === $expectedErrorNumber && preg_match($expectedErrorStringRegex, $error['errstr'])) {
return $this->assertTrue(true);
}
}

$this->fail(sprintf('Error with level "%d" matching "%s" was not triggered', $expectedErrorNumber, $expectedErrorStringRegex));
}

public function testConstructor()
Expand Down Expand Up @@ -195,6 +215,14 @@ public function testCreateCurlOptionsWithBodyAndHeaders()
], $result);
}

public function testMakeRequestWithUntrustedRootCert()
{
$client = new Client('https://untrusted-root.badssl.com/');
$client->makeRequest('GET', 'https://untrusted-root.badssl.com/');

$this->assertError('/SSL certificate problem/i', E_USER_ERROR);
}

/**
* @param object $obj
* @param string $name
Expand Down

0 comments on commit 2c4d52b

Please sign in to comment.