Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NathanIsaac's avatar

Handling Redis Session Expections

I would like to set up Redis to handle sessions, however, I found an issue. When I stop the Redis service I get a Predis \ Connection \ ConnectionException which makes sense, however, on a production server I want to handle this type of error better for users. For example, display my custom error page instead of the default error page that comes with Laravel for production errors and log the error.

I thought about adding a try catch to the index.php file, however, that is adding error logic in two places (global.php and index.php). I'm not sure what is the best way to go here.

Is there a way to make sure all system/php/laravel errors go through the global.php exception handling logic?

Would like to hear any thoughts on how to resolve this issue. Thanks.

In case there is an issue replicating this issue here is some meta information.

Code snippets

// index.php
// Current fix
try {
    $app->run();
}
catch (Exception $e)
{
    // handle exception
}

// global.php
// Would rather handle all errors here
App::error(function(Exception $exception, $code)
{
    // some logic...
    return Response::view('custom-error-page', $data, $code);
});
0 likes
3 replies
mikeziri's avatar

I also had that problem.

you have to catch ConnectionException instead of Exception on Redis calls.

I thought that ConnectionException extended Exception but it first extends CommunicationException, then PredisException and finally the "base class for all Exceptions" \Exception.

not sure if that's the problem but catching Exception instead of ConnectionException doesn't seem to catch it.

I think I didn't try catching \Exception specifically though...

NathanIsaac's avatar

I tried to add the following code snippet to the global.php file, however, I believe the app cycle is dying before it even gets to the global.php file. The reason for this is because I am using redis for session. Any thoughts on how to get around that? Thanks.

App::error(function(Predis\Connection\ConnectionException $e) {
   dd($e->getMessage());
});
opb's avatar

From your original post, you mention that it shows the default error page that comes with Laravel. If it's showing a Laravel error page, then it will be getting to global. Make sure you put the more specific App::error() lines before the one for the generic Exception class. I believe it will match in the order they are put into the file.

Please or to participate in this conversation.