diff --git a/src/Inline/Renderer/SoundCloudRenderer.php b/src/Inline/Renderer/SoundCloudRenderer.php index 03c883b..3223dca 100644 --- a/src/Inline/Renderer/SoundCloudRenderer.php +++ b/src/Inline/Renderer/SoundCloudRenderer.php @@ -3,6 +3,7 @@ namespace JohnnyHuy\Laravel\Inline\Renderer; use ErrorException; +use Exception; use JohnnyHuy\Laravel\Inline\Element\SoundCloud; use League\CommonMark\ElementRendererInterface; use League\CommonMark\HtmlElement; @@ -37,8 +38,8 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen // Seems that the used SoundCloud url is invalid // or SoundCloud is currently not available - if ($soundCloud === null) { - throw new ErrorException('SoundCloud request returned null: ' . $url); + if (empty($soundCloud)) { + return $soundCloud; } // Parse the embed response @@ -54,6 +55,10 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen */ public function getContent(string $url): string { - return file_get_contents($url); + try { + return file_get_contents($url); + } catch (Exception $exception) { + return ''; + } } } diff --git a/tests/Elements/Inline/SoundCloudTest.php b/tests/Elements/Inline/SoundCloudTest.php index 5cf027c..a67ebf4 100644 --- a/tests/Elements/Inline/SoundCloudTest.php +++ b/tests/Elements/Inline/SoundCloudTest.php @@ -109,4 +109,20 @@ public function testShouldNotRender($input, $output) // Arrange $this->assertSame("$output\n", $html); } + + public function testShouldReturnEmptyOnErrorURL() + { + // Arrange + $environment = Environment::createCommonMarkEnvironment(); + $parser = new DocParser($environment); + $htmlRenderer = new HtmlRenderer($environment); + $environment->addInlineParser(new SoundCloudParser()); + $environment->addInlineRenderer(SoundCloud::class, new SoundCloudRenderer() ); + + // Act + $html = $htmlRenderer->renderBlock($parser->parse(':soundcloud http://soundcloud.com/123123/123123123')); + + // Arrange + $this->assertEquals("
\n", $html); + } } \ No newline at end of file