Skip to content

Commit 99bcde8

Browse files
committed
Don't throw an exception on SoundCloud
1 parent d5bed4a commit 99bcde8

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Inline/Renderer/SoundCloudRenderer.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace JohnnyHuy\Laravel\Inline\Renderer;
44

55
use ErrorException;
6+
use Exception;
67
use JohnnyHuy\Laravel\Inline\Element\SoundCloud;
78
use League\CommonMark\ElementRendererInterface;
89
use League\CommonMark\HtmlElement;
@@ -37,8 +38,8 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen
3738

3839
// Seems that the used SoundCloud url is invalid
3940
// or SoundCloud is currently not available
40-
if ($soundCloud === null) {
41-
throw new ErrorException('SoundCloud request returned null: ' . $url);
41+
if (empty($soundCloud)) {
42+
return $soundCloud;
4243
}
4344

4445
// Parse the embed response
@@ -54,6 +55,10 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen
5455
*/
5556
public function getContent(string $url): string
5657
{
57-
return file_get_contents($url);
58+
try {
59+
return file_get_contents($url);
60+
} catch (Exception $exception) {
61+
return '';
62+
}
5863
}
5964
}

tests/Elements/Inline/SoundCloudTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,20 @@ public function testShouldNotRender($input, $output)
109109
// Arrange
110110
$this->assertSame("$output\n", $html);
111111
}
112+
113+
public function testShouldReturnEmptyOnErrorURL()
114+
{
115+
// Arrange
116+
$environment = Environment::createCommonMarkEnvironment();
117+
$parser = new DocParser($environment);
118+
$htmlRenderer = new HtmlRenderer($environment);
119+
$environment->addInlineParser(new SoundCloudParser());
120+
$environment->addInlineRenderer(SoundCloud::class, new SoundCloudRenderer() );
121+
122+
// Act
123+
$html = $htmlRenderer->renderBlock($parser->parse(':soundcloud http://soundcloud.com/123123/123123123'));
124+
125+
// Arrange
126+
$this->assertEquals("<p></p>\n", $html);
127+
}
112128
}

0 commit comments

Comments
 (0)