Laravel: Avoid Try-Catch In Every Method (What To Do Instead)

preview_player
Показать описание
I want to show you a non-ideal try-catch-exception approach some developers take and how to globally override the exceptions in the Laravel exception handler.

Links mentioned in the video:

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

With amazing accuracy, Pavillas talks about what I need today. Do you have a crystal ball?
When will the course "Predictions"? "A personal horoscope for a developer"?

webdev
Автор

Following is my approach.
Exceptions are (as their name implies) something that happens exceptionally on the server side. Therefore my approach is to
1. Use a global exception handler.
2. Log all the details on the server
3. Send a meaningful message to the user, explaining what is expected from the user. This should include whether the action may be retried or not and a 'reference id' of the details logged at the server. Never should the message to the user expose the internal workings of the application (such as database errors). Exposing server details is both meaningless and insecure.
Situations like validations errors on user input should not be handled via exceptions, as they are not exceptional and fit into a normal request/response behavior.

Br,
Luc

VKLuc
Автор

My approach is to add a render method on my exceptions, so I don't have a huge exception handler. Laravel uses the render method of my exceptions or I use try/catch if needed

stunext
Автор

The best way to bring your exceptions under control is using @throws annotation with PHPStan (or Larastan if using Laravel). You are then forced to document or handle your exceptions at the right level, or let them bubble up past your controller and to the handler.

bbbbburton
Автор

Try catch in only controller method but in some cases try catch is used in service layer for certain purpose.

SussanRai
Автор

I use try catch so I can put a breakpoint when debugging into the catch part and then I can read error message when error happens.

tomasfigura
Автор

I'm not good in errors handling but i looking my classes where they meet especially when i use services class then i make exception

mahmoud-bakheet
Автор

what if I want to use DB::transaction() in controller how to combine that with exception handling globally

someone-jqlc
Автор

I use DB transactions in my controller methods and in the catch block, rollback on any kind of exception. What do I do in this case if you advise not to use try catch in controller methods

NanaYaw
Автор

laravel try catch sometime it has "Exception" OR "Throwable". Is there any difference between them?

kailasbedarkar
Автор

Exception classes were made for... EXCEPTIONS. Things that your program is not expected to handle! And you're not supposed to misuse exceptions to hide bugs -- you need them to blow up in a big way. It's the whole point of them: If there's a problem with the system, it should be fixed. Not hidden!

And Exceptions should be LOGGED.

The user is not expected to deal with them, you, the devs, is! Error codes are for YOU, not the user!

johnnyw
Автор

What theme editor is it? Its very clean and readable, how can i get ur .editorconfig?

igerardogc