Skip to content

Commit

Permalink
Use ErrorPlugin to raise 4xx and 5xx exceptions (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielAnca authored and choran committed Jan 18, 2019
1 parent 362e305 commit 1a52946
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Official PHP bindings to the Intercom API

This library supports PHP 7.1 and later

This library uses [HTTPPlug](https://github.com/php-http/httplug) as HTTP client. HTTPPlug is an abstraction that allows this library to support many different HTTP Clients. Therefore, you need to provide it with an adapter for the HTTP library you prefer. You can find all the available adapters [in Packagist](https://packagist.org/providers/php-http/client-implementation). This documentation assumes you use the Guzzle6 Client, but you can replace it with any adapter that you prefer.
This library uses [HTTPlug](https://github.com/php-http/httplug) as HTTP client. HTTPlug is an abstraction that allows this library to support many different HTTP Clients. Therefore, you need to provide it with an adapter for the HTTP library you prefer. You can find all the available adapters [in Packagist](https://packagist.org/providers/php-http/client-implementation). This documentation assumes you use the Guzzle6 Client, but you can replace it with any adapter that you prefer.

The recommended way to install intercom-php is through [Composer](https://getcomposer.org):

Expand Down Expand Up @@ -457,7 +457,7 @@ while (!empty($resp->scroll_param) && sizeof($resp->users) > 0) {

## Exceptions

Exceptions are handled by HTTPPlug. Every exception thrown implements `Http\Client\Exception`. See the different exceptions that can be thrown [in the HTTPPlug documentation](http://docs.php-http.org/en/latest/httplug/exceptions.html).
Exceptions are handled by HTTPlug. Every exception thrown implements `Http\Client\Exception`. See the [http client exceptions](http://docs.php-http.org/en/latest/httplug/exceptions.html) and the [client and server errors](http://docs.php-http.org/en/latest/plugins/error.html).
The Intercom API may return an unsuccessful HTTP response, for example when a resource is not found (404).
If you want to catch errors you can wrap your API call into a try/catch block:

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
"php-http/client-implementation": "*",
"php-http/discovery": "^1.4",
"php-http/message": "^1.7",
"psr/http-message": "^1.0"
"psr/http-message": "^1.0",
"php-http/client-common": "^1.9"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"squizlabs/php_codesniffer": "^3.1",
"php-http/guzzle6-adapter": "^2.0"
"php-http/guzzle6-adapter": "^1.0 || ^2.0"
}
}
23 changes: 18 additions & 5 deletions src/IntercomClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Intercom;

use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
Expand All @@ -11,15 +14,14 @@
use Http\Message\RequestFactory;
use Http\Message\UriFactory;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;

class IntercomClient
{
/**
* @var ClientInterface $httpClient
* @var HttpClient $httpClient
*/
private $httpClient;

Expand Down Expand Up @@ -145,17 +147,17 @@ public function __construct($appIdOrToken, $password = null, $extraRequestHeader
$this->passwordPart = $password;
$this->extraRequestHeaders = $extraRequestHeaders;

$this->httpClient = HttpClientDiscovery::find();
$this->httpClient = $this->getDefaultHttpClient();
$this->requestFactory = MessageFactoryDiscovery::find();
$this->uriFactory = UriFactoryDiscovery::find();
}

/**
* Sets the HTTP client.
*
* @param ClientInterface $httpClient
* @param HttpClient $httpClient
*/
public function setHttpClient(ClientInterface $httpClient)
public function setHttpClient(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}
Expand Down Expand Up @@ -260,6 +262,17 @@ public function getRateLimitDetails()
return $this->rateLimitDetails;
}

/**
* @return HttpClient
*/
private function getDefaultHttpClient()
{
return new PluginClient(
HttpClientDiscovery::find(),
[new ErrorPlugin()]
);
}

/**
* @return array
*/
Expand Down
45 changes: 45 additions & 0 deletions test/IntercomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeImmutable;
use Http\Adapter\Guzzle6\Client;
use Http\Client\Exception;
use Intercom\IntercomClient;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
Expand Down Expand Up @@ -95,6 +96,50 @@ public function testClientWithExtraHeaders()
}
}

public function testClientErrorHandling()
{
$mock = new MockHandler([
new Response(404)
]);

$container = [];
$history = Middleware::history($container);
$stack = HandlerStack::create($mock);
$stack->push($history);

$httpClient = new Client(new GuzzleClient(['handler' => $stack]));

$client = new IntercomClient('u', 'p');
$client->setHttpClient($httpClient);

$this->expectException(Exception::class);
$client->users->create([
'email' => 'test@intercom.io'
]);
}

public function testServerErrorHandling()
{
$mock = new MockHandler([
new Response(500)
]);

$container = [];
$history = Middleware::history($container);
$stack = HandlerStack::create($mock);
$stack->push($history);

$httpClient = new Client(new GuzzleClient(['handler' => $stack]));

$client = new IntercomClient('u', 'p');
$client->setHttpClient($httpClient);

$this->expectException(Exception::class);
$client->users->create([
'email' => 'test@intercom.io'
]);
}

public function testPaginationHelper()
{
$mock = new MockHandler([
Expand Down

0 comments on commit 1a52946

Please sign in to comment.