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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I want to quickly talk about exceptions and try catch blocks in laravel and in PHP based on one lyrica's Forum question so I've seen over my career some people get overly cautious with exceptions so they have read about try catch about exceptions and they try to apply them everywhere and this is one of the examples and I will show you what to do instead based on the documentation and other form answers and also my own course so this person asking a question is trying to put a try catch Block in every method of every controller which is a good intention so if something goes wrong in any of the method you return the Json with the error message but there are things to improve here so imagine create form this response if something goes wrong and throwable exception which means any exception than this error and then this error so the first thing I want to say about that is pretty pointless you don't specify a specific error it's just there was an error 500 it's the same as throwing internal server error in the browser with 500 which means your website user or your API user wouldn't even know what to do with that error so you would need to provide a more meaningful message than this and probably something out of 400 codes like 422 for validation which means it's better to have additional work here somewhere or in the form request class in validation to validate the data so to minimize the possibility of that exception to happen so I would even call that kind of a lazy mode so catch everything show that there was an error and then wishing good luck to the user how to overcome that error and even here catch throwable there may be many exception happening SQL error records not found something not uploading somewhere so that's why people came up with the concept of x exception and it's not any exception you can create your own exception classes you can use laravel or Symphony or PHP exception classes and in case of different exceptions you can provide different messages that's what exceptions are for not just for doing try catch throwable so from that point of view this approach is pretty meaningless to me although it comes from good intention informing the user that the error happened and this is what we've been talking about in our course I teamed up with my colleague modestas to create this course handling exceptions and errors in laravel and as usual it becomes a tradition it started as a tutorial but then it grew so big into various topics so we decided to form it as a course with 5000 words and we're talking about specific exceptions how to create them how to catch them how to lock them and stuff like that so if you want to dive deeper I will link the course in the description below but if you just want to know the basics of how to catch exceptions in in these situations I will show you the official variable documentation what the main things that you need to know so if you want to render the exception so something happened and you want to return Json for example you can do something like this in case of some your specific exception happening you can provide that as renderable and response whatever you want response Json response with blade view if you want to return status code you want so this should happen in the exception Handler so this way you're trying to catch a specific exception but then another way for example perhaps even more used in practice from what I've seen renderable for example not found exception standardized the error message for apis so if the request comes from API then you return record not found instead of the default message by laravel or eloquent which means instead of doing try catch in every method you just do your things in controllers again ideally providing validation before those actions and then if something does go wrong then your exception is overridden in the exception Handler another way provided in the same form topic by no last name is creating a middleware so middleware with try catch would do basically the same thing and you would need to assign that middleware to all for example web requests or you could assign that only to some requests or route group or something like that which makes it a bit more flexible so you can apply that logic only to some of the routes I hope this video gets you a better idea of try catching exceptions if you have any more questions let's discuss in the comments that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 27,065
Rating: undefined out of 5
Keywords:
Id: eTOScyTCkiY
Channel Id: undefined
Length: 4min 44sec (284 seconds)
Published: Fri Aug 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.