Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace Tristanbes\ElophantBundle\EventListener;
use Guzzle\Http\Exception\BadResponseException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Tristanbes\ElophantBundle\Manager\StatsManager;
/**
* Class GuzzleExceptionListener
*/
class GuzzleExceptionListener
{
private $statsManager;
private $fail = false;
/**
* Constructor
*
* @param StatsManager $manager The stats Manager
*/
public function __construct(StatsManager $manager)
{
$this->statsManager = $manager;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof BadResponseException) {
$this->fail = true;
$response = new JsonResponse(array('success' => false));
$event->setResponse($response);
}
}
public function onKernelTerminate()
{
if (false === $this->fail) {
return;
}
$this->statsManager->addFail();
}
}
<service id="tristanbes_elophant.guzzle_exception_eventlistener" class="%tristanbes_elophant.guzzle.exception.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
<tag name="kernel.event_listener" event="kernel.terminate" method="onKernelTerminate" />
<argument type="service" id="tristanbes_elophant.stats.manager" />
</service>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.