Laravel Try-Catch with Exceptions: Examples from Open-Source

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I want to present another new course released on laravel dailycom for premium members this is a text-based course in this case about exceptions and errors in laravel pretty often I get questions about exceptions and how to use them and how to use try catch and other similar syntax options and when to use them so I decided to team up with my colleague modestas and prepare a course and as usual on YouTube I release a few lessons for you for free even if you're not a premium member so in this case I will release two lessons a bit untypical Choice it will be the first lesson and the last lesson so today let's take a look at open source examples of using try catch with exceptions and if you want the full course the link will be as usual in the description below so I will read and summarize this lesson for you in a video format so our goal as a developer is to prevent this screen from happening you should provide a typical visual error message valid validation message or something like that some redirect instead of typical exception page or on production you would see something like this just 500 server error even without any explanation which is good actually because your end user should not see any technical details that's why you need to in your EnV file dot EnV file provide app EnV production and app debug false then they would see the standard error but our goal is to prevent that to have try catch and then on catch specify a good error message understandable error message and how to fix it so this is a typical how to handle exception try catch blog so you try some code and in case of exception you for example log some error somewhere but then the question is what else can we do here I've seen a lot of tutorials with try catch with just login exception or with just meaningless exit or something like that so let's take a look at open source examples in a few re real projects and the first one is bagisto open source e-commerce platform which has this function so we're trying to add a product to the card but if something happens we're catching the general exception in this case we are flashing the warning with error message we log the error we try to find the product and we redirect to that product URL key whatever that is so in other words we're overriding the response and we have the response here this code is hidden there's much more code here so at some point we have the response from the try block and in the catch block we're overriding that response to be returned here so this is one example what you can do in the catch block just override the default values that would be returned from the function another example in the same project in the menu generator it's a blade file and maybe it's not the best example to show PHP code in the blade because that logic should be some somewhere else not in the blade but we have try catch here and let's see what it does so try to populate the sub menu collection but if something goes wrong we just override Sub menu two menu item children so some logic which may fail and then there's a fallback value to that Sub menu collection another example is from 12 CMS in the search function they have some collection magic so to speak and here's the try catch we try to assign the author to latest user otherwise admin and if something goes wrong somewhere in those relationships we still provide the fallback of admin technically this should not really happen if you provide good relationships here or you may use no safe operator in PHP so add a question mark here and here and here but this is just another example of providing fallback value to the track right main block variable another example is in front-end helpers of the same project we try to populate some string value for front and assets otherwise we return just the file again it's about returning or assigning some fallback value in the catch block and another example pretty typical actually in laravel catching any exception or Global PHP exception and overriding that with laravel exception typically validation exception so larval allows you to throw validation exception which would be processed internally bilateral and returning redirect back or returning 42 status code if it's an API project you just provide with messages like this so if something goes wrong with uploading the Avatar here then instead of throwing General exception or any exception we just return validation message which is more user-friendly so these are the examples of what you can do in try catch blog from real open source projects what would be your use cases what do you do in the catch blog share your experience in the comments below and tomorrow we will continue with second free lesson from this course which is handling exceptions in laravel API so how to override the default find or fail error message and if you want the full course as usual the link will be in the description below that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 4,647
Rating: undefined out of 5
Keywords:
Id: XEw-2Z8bQyg
Channel Id: undefined
Length: 5min 31sec (331 seconds)
Published: Tue Jun 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.