Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch exception when Laravel cannot connect to Redis?

Tags:

redis

laravel

Iam using Redis as cache in Laravel.

But I cannot catch Exception when laravel cannot connect to Redis (redis stopped, ...). I want catch this exception to reponse to client by JSON.

How can I do it ?

like image 493
Quy Nguyen Vu Avatar asked Dec 08 '25 16:12

Quy Nguyen Vu


1 Answers

You can use render() function of App\Exceptions\Handler class as:

public function render($request, Exception $exception)
{
    if ($exception instanceof SomeRedisException) {
        return response()->json('Redis Error',500);
    }

    return parent::render($request, $exception);
}

Docs

like image 163
Amit Gupta Avatar answered Dec 10 '25 06:12

Amit Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!