Home » Php » php – Is try catch block necessary inside controller in laravel?

php – Is try catch block necessary inside controller in laravel?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m currently working on a project with my friend. He follows this method of writing codes. Is it good or bad? I’m also not that much experience developer.

public function store(Request $request)
 {  
    try   
    {  
        $comment = new TicketComment();  
        $comment->content = $request['comment'];  
        $comment->user_id = Auth::user()->id;  
        $comment->ticket_id = $request['ticketId'];  
        $comment->save();  
        $ticket = Ticket::where('id', '=', $comment->ticket_id)->first();  
        $ticket->updated_at = $comment->created_at;  
        $ticket->update();  
    }    
    catch(Exception $e)  
    {  
        request()->session()->flash('unsuccessMessage', 'Failed to add comment !!!');  
        return redirect()->back();  
    }  
    request()->session()->flash('successMessage', 'Comment has been successfully added !!!');  
    return redirect()->back();  
}  
How to&Answers:

It is allways good to handle the errors. However, Laravel has built in error handling, which ease this process. This does not mean you dont need the try catch block.
Read more about laravel error handling: https://laravel.com/docs/5.8/errors

Answer:

Don’t have a broad Exception catch block. Only catch exceptions you expect to be thrown in that block that way you can properly log unexpected exceptions and fix any other bugs in your code that may have caused those, instead of hiding them from yourself.

If you must do this then it might be in the context of something like:

public function store(Request $request)
 {  
    try   
    {  
        $comment = new TicketComment();  
        $comment->content = $request['comment'];  
        $comment->user_id = Auth::user()->id;  
        $comment->ticket_id = $request['ticketId'];  
        $comment->save();  
        $ticket = Ticket::where('id', '=', $comment->ticket_id)->first();  
        $ticket->updated_at = $comment->created_at;  
        $ticket->update();  
    }    
    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('unsuccessMessage', 'Failed to add comment !!!');  
        return redirect()->back();  

    }  
    request()->session()->flash('successMessage', 'Comment has been successfully added !!!');  
    return redirect()->back();  
}  

This way any unexpected exceptions will still be reported and you can go through your logs later to fix any bugs that are causing them.

As a reminder since PHP 7.1 you can have a union of exceptions in a catch block (Reference) e.g.

try { } 
catch (ExceptionType1|ExceptionType2 $e) {

}

This way you can handle the exceptions you know you can handle and let Laravel handle the ones you are not sure how to handle.

Note that you generally don’t have to have try-catch blocks in controller code, you can always use the exception handler to do the flash/redirect back combination for all unhandled exceptions if that is your preferred way of handling them.