diff --git a/src/Illuminate/Broadcasting/BroadcastManager.php b/src/Illuminate/Broadcasting/BroadcastManager.php index bbd88792c72f..acb01d7bd31d 100644 --- a/src/Illuminate/Broadcasting/BroadcastManager.php +++ b/src/Illuminate/Broadcasting/BroadcastManager.php @@ -21,6 +21,8 @@ use InvalidArgumentException; use Psr\Log\LoggerInterface; use Pusher\Pusher; +use RuntimeException; +use Throwable; /** * @mixin \Illuminate\Contracts\Broadcasting\Broadcaster @@ -292,7 +294,11 @@ protected function resolve($name) throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); } - return $this->{$driverMethod}($config); + try { + return $this->{$driverMethod}($config); + } catch (Throwable $e) { + throw new RuntimeException("Failed to create broadcaster for connection \"{$name}\" with error: {$e->getMessage()}.", 0, $e); + } } /** diff --git a/tests/Integration/Broadcasting/BroadcastManagerTest.php b/tests/Integration/Broadcasting/BroadcastManagerTest.php index 2f94ec2c2c7d..b85c102d7d07 100644 --- a/tests/Integration/Broadcasting/BroadcastManagerTest.php +++ b/tests/Integration/Broadcasting/BroadcastManagerTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Integration\Broadcasting; use Illuminate\Broadcasting\BroadcastEvent; +use Illuminate\Broadcasting\BroadcastException; use Illuminate\Broadcasting\BroadcastManager; use Illuminate\Broadcasting\UniqueBroadcastEvent; use Illuminate\Config\Repository; @@ -17,6 +18,7 @@ use Illuminate\Support\Facades\Queue; use InvalidArgumentException; use Orchestra\Testbench\TestCase; +use RuntimeException; class BroadcastManagerTest extends TestCase { @@ -100,6 +102,35 @@ public function testThrowExceptionWhenUnknownStoreIsUsed() $broadcastManager->connection('alien_connection'); } + public function testThrowExceptionWhenDriverCreationFails() + { + $userConfig = [ + 'broadcasting' => [ + 'connections' => [ + 'log_connection_1' => [ + 'driver' => 'log', + ], + ], + ], + ]; + + $app = $this->getApp($userConfig); + $app->singleton(\Psr\Log\LoggerInterface::class, function () { + throw new \RuntimeException('Logger service not available'); + }); + + $broadcastManager = new BroadcastManager($app); + + try { + $broadcastManager->connection('log_connection_1'); + $this->fail('Expected BroadcastException was not thrown'); + } catch (RuntimeException $e) { + $this->assertStringContainsString('Failed to create broadcaster for connection "log_connection_1"', $e->getMessage()); + $this->assertStringContainsString('Logger service not available', $e->getMessage()); + $this->assertInstanceOf(\RuntimeException::class, $e->getPrevious()); + } + } + protected function getApp(array $userConfig) { $app = new Container;