Skip to content

Commit

Permalink
Don't throw an exception on SoundCloud
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyhuy committed Jan 5, 2020
1 parent d5bed4a commit 99bcde8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Inline/Renderer/SoundCloudRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 '';
}
}
}
16 changes: 16 additions & 0 deletions tests/Elements/Inline/SoundCloudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("<p></p>\n", $html);
}
}

0 comments on commit 99bcde8

Please sign in to comment.