Skip to content

Throw InvalidRequest exception on invalid CURL request #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a name="usage"></a>
# Usage

Expand Down
7 changes: 7 additions & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace SendGrid;

use SendGrid\Exception\InvalidRequest;

/**
*
* Class Client
Expand Down Expand Up @@ -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)
{
Expand All @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions lib/Exception/InvalidRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* HTTP Client library
*
* @author Matt Bernier <[email protected]>
* @author Elmer Thomas <[email protected]>
* @copyright 2018 SendGrid
* @license https://opensource.org/licenses/MIT The MIT License
* @version GIT: <git_id>
* @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);
}

}
9 changes: 9 additions & 0 deletions test/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SendGrid\Test;

use SendGrid\Client;
use SendGrid\Exception\InvalidRequest;

class ClientTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -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
Expand Down