Be more specific with what exception we catch

This commit is contained in:
Tobias Nyholm 2018-09-04 08:42:35 +02:00
parent b1aa0047fd
commit fb30c77659
No known key found for this signature in database
GPG Key ID: B14C3A029D1831F6
2 changed files with 21 additions and 2 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Formatter;
use Symfony\Component\Translation\Exception\LogicException;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
class FallbackFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
{
@ -35,7 +36,7 @@ class FallbackFormatter implements MessageFormatterInterface, ChoiceMessageForma
{
try {
$result = $this->firstFormatter->format($message, $locale, $parameters);
} catch (\Throwable $e) {
} catch (InvalidArgumentException $e) {
return $this->secondFormatter->format($message, $locale, $parameters);
}
@ -52,7 +53,7 @@ class FallbackFormatter implements MessageFormatterInterface, ChoiceMessageForma
if ($this->firstFormatter instanceof ChoiceMessageFormatterInterface && $this->secondFormatter instanceof ChoiceMessageFormatterInterface) {
try {
$result = $this->firstFormatter->choiceFormat($message, $number, $locale, $parameters);
} catch (\Throwable $e) {
} catch (InvalidArgumentException $e) {
return $this->secondFormatter->choiceFormat($message, $number, $locale, $parameters);
}

View File

@ -17,6 +17,7 @@ use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface;
use Symfony\Component\Translation\Formatter\FallbackFormatter;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
class FallbackFormatterTest extends \PHPUnit\Framework\TestCase
{
public function testFormatSame()
@ -74,6 +75,23 @@ class FallbackFormatterTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('bar', (new FallbackFormatter($first, $second))->format('foo', 'en', array(2)));
}
public function testFormatExceptionUnknown()
{
$first = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock();
$first
->expects($this->once())
->method('format')
->willThrowException(new \RuntimeException());
$second = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock();
$second
->expects($this->exactly(0))
->method('format');
$this->expectException(\RuntimeException::class);
$this->assertEquals('bar', (new FallbackFormatter($first, $second))->format('foo', 'en', array(2)));
}
public function testChoiceFormatSame()
{
$first = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock();