8000 Be more specific with what exception we catch · symfony/symfony@fb30c77 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb30c77

Browse files
committed
Be more specific with what exception we catch
1 parent b1aa004 commit fb30c77

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Symfony/Component/Translation/Formatter/FallbackFormatter.php

Lines changed: 3 additions & 2 deletions
< 10000 td data-grid-cell-id="diff-0bea8fa1af768eaf0eb291c0dd81727c0e604a9119dd60271e1c317624206eb9-15-16-0" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Translation\Formatter;
1313

1414
use Symfony\Component\Translation\Exception\LogicException;
15+
use Symfony\Component\Translation\Exception\InvalidArgumentException;
16

1617
class FallbackFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
1718
{
@@ -35,7 +36,7 @@ public function format($message, $locale, array $parameters = array())
3536
{
3637
try {
3738
$result = $this->firstFormatter->format($message, $locale, $parameters);
38-
} catch (\Throwable $e) {
39+
} catch (InvalidArgumentException $e) {
3940
return $this->secondFormatter->format($message, $locale, $parameters);
4041
}
4142

@@ -52,7 +53,7 @@ public function choiceFormat($message, $number, $locale, array $parameters = arr
5253
if ($this->firstFormatter instanceof ChoiceMessageFormatterInterface && $this->secondFormatter instanceof ChoiceMessageFormatterInterface) {
5354
try {
5455
$result = $this->firstFormatter->choiceFormat($message, $number, $locale, $parameters);
55-
} catch (\Throwable $e) {
56+
} catch (InvalidArgumentException $e) {
5657
return $this->secondFormatter->choiceFormat($message, $number, $locale, $parameters);
5758
}
5859

src/Symfony/Component/Translation/Tests/Formatter/FallbackFormatterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Translation\Formatter\FallbackFormatter;
1818
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
1919

20+
2021
class FallbackFormatterTest extends \PHPUnit\Framework\TestCase
2122
{
2223
public function testFormatSame()
@@ -74,6 +75,23 @@ public function testFormatException()
7475
$this->assertEquals('bar', (new FallbackFormatter($first, $second))->format('foo', 'en', array(2)));
7576
}
7677

78+
public function testFormatExceptionUnknown()
79+
{
80+
$first = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock();
81+
$first
82+
->expects($this->once())
83+
->method('format')
84+
->willThrowException(new \RuntimeException());
85+
86+
$second = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock();
87+
$second
88+
->expects($this->exactly(0))
89+
->method('format');
90+
91+
$this->expectException(\RuntimeException::class);
92+
$this->assertEquals('bar', (new FallbackFormatter($first, $second))->format('foo', 'en', array(2)));
93+
}
94+
7795
public function testChoiceFormatSame()
7896
{
7997
$first = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock();

0 commit comments

Comments
 (0)
0