Skip to content

Commit

Permalink
properly handle exceptions in translator listener
Browse files Browse the repository at this point in the history
The `setLocale()` method can throw an `\InvalidArgumentException` when
an invalid locale has been passed. These exceptions must be handled by
the `TranslatorListener` which should then pass the request's default
locale instead.
  • Loading branch information
xabbuh committed Oct 8, 2014
1 parent 46d18f4 commit d3a0a55
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
Expand All @@ -36,14 +37,16 @@ public function __construct(TranslatorInterface $translator, RequestStack $reque

public function onKernelRequest(GetResponseEvent $event)
{
$this->translator->setLocale($event->getRequest()->getLocale());
$this->setLocale($event->getRequest());
}

public function onKernelFinishRequest(FinishRequestEvent $event)
{
if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
$this->translator->setLocale($parentRequest->getLocale());
if (null === $parentRequest = $this->requestStack->getParentRequest()) {
return;
}

$this->setLocale($parentRequest);
}

public static function getSubscribedEvents()
Expand All @@ -54,4 +57,13 @@ public static function getSubscribedEvents()
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
);
}

private function setLocale(Request $request)
{
try {
$this->translator->setLocale($request->getLocale());
} catch (\InvalidArgumentException $e) {
$this->translator->setLocale($request->getDefaultLocale());
}
}
}
Expand Up @@ -41,6 +41,21 @@ public function testLocaleIsSetInOnKernelRequest()
$this->listener->onKernelRequest($event);
}

public function testDefaultLocaleIsUsedOnExceptionsInOnKernelRequest()
{
$this->translator
->expects($this->at(0))
->method('setLocale')
->will($this->throwException(new \InvalidArgumentException()));
$this->translator
->expects($this->at(1))
->method('setLocale')
->with($this->equalTo('en'));

$event = new GetResponseEvent($this->createHttpKernel(), $this->createRequest('fr'), HttpKernelInterface::MASTER_REQUEST);
$this->listener->onKernelRequest($event);
}

public function testLocaleIsSetInOnKernelFinishRequestWhenParentRequestExists()
{
$this->translator
Expand All @@ -63,6 +78,22 @@ public function testLocaleIsNotSetInOnKernelFinishRequestWhenParentRequestDoesNo
$this->listener->onKernelFinishRequest($event);
}

public function testDefaultLocaleIsUsedOnExceptionsInOnKernelFinishRequest()
{
$this->translator
->expects($this->at(0))
->method('setLocale')
->will($this->throwException(new \InvalidArgumentException()));
$this->translator
->expects($this->at(1))
->method('setLocale')
->with($this->equalTo('en'));

$this->setMasterRequest($this->createRequest('fr'));
$event = new FinishRequestEvent($this->createHttpKernel(), $this->createRequest('de'), HttpKernelInterface::SUB_REQUEST);
$this->listener->onKernelFinishRequest($event);
}

private function createHttpKernel()
{
return $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
Expand Down

0 comments on commit d3a0a55

Please sign in to comment.