Handle Exception Error In Laravel Using Rescue Helper

Hello Artisan, in this example i am going to discuss about how to handle exception error in laravel. You can say that it is the best way to handle error in Laravel using laravel rescue helper function. So if you wanna learn laravel error handling best practices, then this example is for you.

For showing this example i am going to use Laravel rescue helper. The Laravel rescue function executes the given closure and catches any exceptions that occur during its execution. All those exceptions that are caught will be sent to your exception handler; however, the request will continue processing:

The normal way to handle exception

It is the normal way to handle error or exception. See the example:

public function store(Request $request)
{  
    try   
    {  
        $comment = new Comment();  
        $comment->content = $request['comment'];  
        $comment->user_id = Auth::user()->id;  
        $comment->blog_id = $request['blog_id'];  
        $comment->save(); 
    } catch(Exception $e) { 
        if (!($e instanceof SQLException)) {
            app()->make(\App\Exceptions\Handler::class)->report($e); 
            // Report the exception if you don't know what actually caused it
        }
        request()->session()->flash('error', 'Failed to add comment.');  
        return redirect()->back();
    }
}

 

Now ok, that is perfect. But do you know there’s a cleaner and simple way in Laravel that you can use to make this even shorter using rescue() helper. Laravel provided the rescue() helper where you can pass in the piece of code as a closure for which we wanna handle exceptions.

return rescue(function () {
    return $this->method();
}, function () {
    return $this->failure();
});

 

So now we can handle above code like that easily using rescue() helper:

public function store(Request $request)
{
    rescue(function () {
        $comment = new Comment();  
        $comment->content = $request['comment'];  
        $comment->user_id = Auth::user()->id;  
        $comment->blog_id = $request['blog_id'];  
        $comment->save();
    }, function() {
        request()->session()->flash('error', 'Failed to add comment.');  
        return redirect()->back();
    }, true);
}

 

You know that the rescue() helper can accept three arguments. See the docs of rescue helper from here. If you’re confused about rescue helper, here’s how the function definition of rescue() helper looks like.

/**
 * Catch a potential exception and return a default value.
 *
 * @param  callable  $callback
 * @param  mixed  $rescue
 * @param  bool  $report
 * @return mixed
 */
function rescue(callable $callback, $rescue = null, $report = true)
{
    try {
        return $callback();
    } catch (Throwable $e) {
        if ($report) {
            report($e);
        }

        return $rescue instanceof Closure ? $rescue($e) : $rescue;
    }
}

 

Read also : Laravel Error Handling with Exception and Try Catch

 

Hope it can help you.

 

#laravel #laravel-8x #error-handling #helper