This repository has been archived by the owner on Jan 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from citilinkru/exception_handler
add new feature to handle exceptions without response
- Loading branch information
Showing
4 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Citilink\ExpertSenderApi\Event; | ||
|
||
use Citilink\ExpertSenderApi\RequestInterface; | ||
use GuzzleHttp\Exception\RequestException; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Event on exception thrown while making request | ||
* | ||
* @author Nikita Sapogov <[email protected]> | ||
*/ | ||
class RequestExceptionThrown extends Event | ||
{ | ||
/** | ||
* @var RequestInterface Request | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var RequestException $exception | ||
*/ | ||
private $exception; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param RequestInterface $apiRequest Api request | ||
* @param RequestException $exception Thrown exception | ||
*/ | ||
public function __construct(RequestInterface $apiRequest, RequestException $exception) | ||
{ | ||
$this->request = $apiRequest; | ||
$this->exception = $exception; | ||
} | ||
|
||
/** | ||
* Get api request | ||
* | ||
* @return RequestInterface Api request | ||
*/ | ||
public function getRequest(): RequestInterface | ||
{ | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* Get thrown exception | ||
* | ||
* @return RequestException Thrown exception | ||
*/ | ||
public function getException(): RequestException | ||
{ | ||
return $this->exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,16 @@ | |
namespace Citilink\ExpertSenderApi\Tests; | ||
|
||
use Citilink\ExpertSenderApi\Enum\SubscribersGetRequest\DataOption; | ||
use Citilink\ExpertSenderApi\Event\RequestExceptionThrown; | ||
use Citilink\ExpertSenderApi\Event\ResponseReceivedEvent; | ||
use Citilink\ExpertSenderApi\Model\ErrorMessage; | ||
use Citilink\ExpertSenderApi\Model\SubscribersPostRequest\Identifier; | ||
use Citilink\ExpertSenderApi\Model\SubscribersPostRequest\SubscriberInfo; | ||
use Citilink\ExpertSenderApi\Request\SubscribersGetRequest; | ||
use Citilink\ExpertSenderApi\Request\SubscribersPostRequest; | ||
use Citilink\ExpertSenderApi\RequestSender; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\ConnectException; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
|
@@ -116,4 +119,59 @@ function (ResponseReceivedEvent $event) use (&$receivedEventHasBeenSent) { | |
Assert::assertEquals(null, $apiResponse->getErrorCode()); | ||
Assert::assertCount(0, $apiResponse->getErrorMessages()); | ||
} | ||
|
||
public function testThrowExceptionWithoutResponse() | ||
{ | ||
$mockHandler = new MockHandler( | ||
[ | ||
new ConnectException( | ||
'cURL error 28: SSL connection timeout (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)', | ||
new Request('GET', '/Api/Subscribers') | ||
), | ||
] | ||
); | ||
|
||
$handler = HandlerStack::create($mockHandler); | ||
$client = new Client(['handler' => $handler]); | ||
$eventDispatcher = new EventDispatcher(); | ||
$receivedEventHasBeenSent = false; | ||
$thrownExceptionEventHasBeenSent = false; | ||
$eventDispatcher->addListener( | ||
'expert_sender_api.response.received', | ||
function (ResponseReceivedEvent $event) use (&$receivedEventHasBeenSent) { | ||
$receivedEventHasBeenSent = true; | ||
Assert::assertInstanceOf(ResponseReceivedEvent::class, $event); | ||
} | ||
); | ||
|
||
$eventDispatcher->addListener( | ||
'expert_sender_api.request.exception_thrown', | ||
function (RequestExceptionThrown $event) use (&$thrownExceptionEventHasBeenSent) { | ||
$thrownExceptionEventHasBeenSent = true; | ||
Assert::assertInstanceOf(RequestExceptionThrown::class, $event); | ||
Assert::assertNotNull($event->getRequest()); | ||
Assert::assertNotNull($event->getException()); | ||
} | ||
); | ||
|
||
$requestSender = new RequestSender($client, 'api-key', $eventDispatcher); | ||
$response = $requestSender->send(new SubscribersGetRequest('[email protected]', DataOption::SHORT())); | ||
|
||
Assert::assertTrue($thrownExceptionEventHasBeenSent); | ||
Assert::assertTrue($receivedEventHasBeenSent); | ||
Assert::assertFalse($response->isOk()); | ||
Assert::assertEquals(400, $response->getErrorCode()); | ||
$errorMessages = $response->getErrorMessages(); | ||
Assert::assertCount(1, $errorMessages); | ||
/** @var ErrorMessage $firstErrorMessage */ | ||
$firstErrorMessage = reset($errorMessages); | ||
Assert::assertEquals( | ||
'ES API ARTIFICIAL ERROR: "GuzzleHttp\Exception\ConnectException":"cURL error 28: SSL connection ' | ||
. 'timeout (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)". Please note, that this error ' | ||
. 'indicates about exception without response. You can subscribe for ' | ||
. '"expert_sender_api.request.exception_thrown" event and get more information about exception', | ||
$firstErrorMessage->getMessage() | ||
); | ||
Assert::assertEquals([], $firstErrorMessage->getOptions()); | ||
} | ||
} |