How to catch Laravel BroadcastException?

I have a Laravel 5.5 app, in which I generate events, for eg:

event(new ThingChange($thing, 'foo', 'bar'));

Those events implement ShouldBroadcast:

class ThingChange implements ShouldBroadcast  {

I’m broadcasting to Pusher channels. Broadcasting generates a queued job (I’m using the database queue driver), and the Laravel queue worker handles those jobs. The pusher config in config/broadcasting.php is standard:

'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER', 'mt1'),
'encrypted' => true,
'useTLS' => true
],
],

This all works. It has been running for years, the config is correct. It also works fine from my local dev machine. There are many questions here on SO about getting the connection to Pusher working at all, often solved by allowing unencrypted connections, or adding curl parameters (example, example, example …) – this is not one of those questions.

However every now and then, if there are network troubles, broadcasting (unsurprisingly) fails. Last night for example there were network issues at my hosting provider’s data centre, and I got a few of these exceptions in my logs:

[2021-12-11 00:22:43] production.ERROR: {“exception”:”[object] (IlluminateBroadcastingBroadcastException(code: 0): at /path/to/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:107)

The referenced line 107 throws that exception when Laravel could not connect to Pusher:

throw new BroadcastException(
is_bool($response) ? 'Failed to connect to Pusher.' : $response['body']
);

This is to be expected – network was down, Laravel could not connect to Pusher. There’s no way around this, and I’m not trying to magically work around network trouble.

My question is how can I catch that exception? I monitor my production logs and don’t want to see these – these events are simply notifications, they are not critical, and while I’d like to log that they failed, I don’t want hard ERRORs and multiple full exception traces clogging up my logs.

Trying to catch an exception when generating the event does not work of course, bcs that is not what is actually failing:

try {
event(new ThingChange($thing, 'foo', 'bar'));
} catch (Exception $e) {
// Won't work, firing the event is not what fails ...
}

There is nothing to actually catch in my event class itself, it simply sets some properties for the payload, and defines the channels to broadcast on.

Is there any way to catch this, without modifying Laravel sources?

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/3n9hbBd
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.