Skip to content

Commit add4f4a

Browse files
[12.x] Improve BroadcastManager error messages when trying to get a Broadcaster (#57275)
* [12.x] Improve BroadcastManager error messages when trying to get a Broadcaster Previously, when a broadcaster driver failed to initialize, the error message would only show the underlying exception without indicating which broadcasting connection configuration was responsible, and it was hard to identify it was actually coming from a broadcaster. Missing to provide a value for REVERB_APP_KEY, REVERB_APP_ID or REVERB_APP_SECRET would throw an error such as `TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given` and it would occur after running `composer install`, `php artisan optimize` or any command that would cause the broadcasting to be booted. This change wraps driver creation in a try-catch block that throws a BroadcastException with the connection name, making it immediately clear where the error is coming from and which configuration needs to be fixed. Before: > TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given After: > BroadcastException: Failed to create broadcaster for connection "terminal" with error: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given * [12.x] Improve BroadcastManager error messages when trying to get a Broadcaster Previously, when a broadcaster driver failed to initialize, the error message would only show the underlying exception without indicating which broadcasting connection configuration was responsible, and it was hard to identify it was actually coming from a broadcaster. Missing to provide a value for REVERB_APP_KEY, REVERB_APP_ID or REVERB_APP_SECRET would throw an error such as `TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given` and it would occur after running `composer install`, `php artisan optimize` or any command that would cause the broadcasting to be booted. This change wraps driver creation in a try-catch block that throws a BroadcastException with the connection name, making it immediately clear where the error is coming from and which configuration needs to be fixed. Before: > TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given After: > BroadcastException: Failed to create broadcaster for connection "my-connection" with error: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given * [12.x] Improve BroadcastManager error messages when trying to get a Broadcaster Previously, when a broadcaster driver failed to initialize, the error message would only show the underlying exception without indicating which broadcasting connection configuration was responsible, and it was hard to identify it was actually coming from a broadcaster. Missing to provide a value for REVERB_APP_KEY, REVERB_APP_ID or REVERB_APP_SECRET would throw an error such as `TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given` and it would occur after running `composer install`, `php artisan optimize` or any command that would cause the broadcasting to be booted. This change wraps driver creation in a try-catch block that throws a BroadcastException with the connection name, making it immediately clear where the error is coming from and which configuration needs to be fixed. Before: > TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given After: > BroadcastException: Failed to create broadcaster for connection "my-connection" with error: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given * [12.x] Improve BroadcastManager error messages when trying to get a Broadcaster Previously, when a broadcaster driver failed to initialize, the error message would only show the underlying exception without indicating which broadcasting connection configuration was responsible, and it was hard to identify it was actually coming from a broadcaster. Missing to provide a value for REVERB_APP_KEY, REVERB_APP_ID or REVERB_APP_SECRET would throw an error such as `TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given` and it would occur after running `composer install`, `php artisan optimize` or any command that would cause the broadcasting to be booted. This change wraps driver creation in a try-catch block that throws a BroadcastException with the connection name, making it immediately clear where the error is coming from and which configuration needs to be fixed. Before: > TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given After: > BroadcastException: Failed to create broadcaster for connection "my-connection" with error: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given * [12.x] Improve BroadcastManager error messages when trying to get a Broadcaster Previously, when a broadcaster driver failed to initialize, the error message would only show the underlying exception without indicating which broadcasting connection configuration was responsible, and it was hard to identify it was actually coming from a broadcaster. Missing to provide a value for REVERB_APP_KEY, REVERB_APP_ID or REVERB_APP_SECRET would throw an error such as `TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given` and it would occur after running `composer install`, `php artisan optimize` or any command that would cause the broadcasting to be booted. This change wraps driver creation in a try-catch block that throws a BroadcastException with the connection name, making it immediately clear where the error is coming from and which configuration needs to be fixed. Before: > TypeError: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given After: > BroadcastException: Failed to create broadcaster for connection "my-connection" with error: Pusher\Pusher::__construct(): Argument #3 ($app_id) must be of type string, null given Testing using the LogBroadcaster since it is always available, while Pusher\Pusher is not. * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 92ea383 commit add4f4a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Illuminate/Broadcasting/BroadcastManager.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use InvalidArgumentException;
2222
use Psr\Log\LoggerInterface;
2323
use Pusher\Pusher;
24+
use RuntimeException;
25+
use Throwable;
2426

2527
/**
2628
* @mixin \Illuminate\Contracts\Broadcasting\Broadcaster
@@ -292,7 +294,11 @@ protected function resolve($name)
292294
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
293295
}
294296

295-
return $this->{$driverMethod}($config);
297+
try {
298+
return $this->{$driverMethod}($config);
299+
} catch (Throwable $e) {
300+
throw new RuntimeException("Failed to create broadcaster for connection \"{$name}\" with error: {$e->getMessage()}.", 0, $e);
301+
}
296302
}
297303

298304
/**

tests/Integration/Broadcasting/BroadcastManagerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Integration\Broadcasting;
44

55
use Illuminate\Broadcasting\BroadcastEvent;
6+
use Illuminate\Broadcasting\BroadcastException;
67
use Illuminate\Broadcasting\BroadcastManager;
78
use Illuminate\Broadcasting\UniqueBroadcastEvent;
89
use Illuminate\Config\Repository;
@@ -17,6 +18,7 @@
1718
use Illuminate\Support\Facades\Queue;
1819
use InvalidArgumentException;
1920
use Orchestra\Testbench\TestCase;
21+
use RuntimeException;
2022

2123
class BroadcastManagerTest extends TestCase
2224
{
@@ -100,6 +102,35 @@ public function testThrowExceptionWhenUnknownStoreIsUsed()
100102
$broadcastManager->connection('alien_connection');
101103
}
102104

105+
public function testThrowExceptionWhenDriverCreationFails()
106+
{
107+
$userConfig = [
108+
'broadcasting' => [
109+
'connections' => [
110+
'log_connection_1' => [
111+
'driver' => 'log',
112+
],
113+
],
114+
],
115+
];
116+
117+
$app = $this->getApp($userConfig);
118+
$app->singleton(\Psr\Log\LoggerInterface::class, function () {
119+
throw new \RuntimeException('Logger service not available');
120+
});
121+
122+
$broadcastManager = new BroadcastManager($app);
123+
124+
try {
125+
$broadcastManager->connection('log_connection_1');
126+
$this->fail('Expected BroadcastException was not thrown');
127+
} catch (RuntimeException $e) {
128+
$this->assertStringContainsString('Failed to create broadcaster for connection "log_connection_1"', $e->getMessage());
129+
$this->assertStringContainsString('Logger service not available', $e->getMessage());
130+
$this->assertInstanceOf(\RuntimeException::class, $e->getPrevious());
131+
}
132+
}
133+
103134
protected function getApp(array $userConfig)
104135
{
105136
$app = new Container;

0 commit comments

Comments
 (0)