Skip to content

Commit c14c696

Browse files
committed
[Icons] Do not fail application if there is not internet connection
1 parent cd4dbe3 commit c14c696

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Icons/src/Twig/UXIconRuntime.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\Icons\Twig;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\HttpClient\Exception\TransportException;
1516
use Symfony\UX\Icons\Exception\IconNotFoundException;
1617
use Symfony\UX\Icons\IconRendererInterface;
1718
use Twig\Extension\RuntimeExtensionInterface;
@@ -45,6 +46,10 @@ public function renderIcon(string $name, array $attributes = []): string
4546
}
4647

4748
throw $e;
49+
} catch (TransportException $e) {
50+
$this->logger?->warning($e->getMessage());
51+
52+
return '';
4853
}
4954
}
5055

src/Icons/tests/Unit/Twig/UXIconRuntimeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\LoggerInterface;
16+
use Symfony\Component\HttpClient\Exception\TransportException;
1617
use Symfony\UX\Icons\Exception\IconNotFoundException;
1718
use Symfony\UX\Icons\IconRendererInterface;
1819
use Symfony\UX\Icons\Twig\UXIconRuntime;
@@ -40,4 +41,19 @@ public function testRenderIconIgnoreNotFound()
4041
$this->expectException(IconNotFoundException::class);
4142
$runtime->renderIcon('not_found');
4243
}
44+
45+
public function testRenderIconReturnsEmptyStringIfConnectionIsNotPossible()
46+
{
47+
$renderer = $this->createMock(IconRendererInterface::class);
48+
$renderer->method('renderIcon')
49+
->willThrowException(new TransportException('Could not resolve host api.iconify.design'));
50+
51+
$logger = $this->createMock(LoggerInterface::class);
52+
$logger->expects($this->once())
53+
->method('warning')
54+
->with('Could not resolve host api.iconify.design');
55+
56+
$runtime = new UXIconRuntime($renderer, true, $logger);
57+
$this->assertEquals('', $runtime->renderIcon('tabler:search'));
58+
}
4359
}

0 commit comments

Comments
 (0)