Laravel API: Override "Model Not Found" with Exception

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today we have the second free video from latest course on laravel exceptions and errors the first one was yesterday if you missed that I will link that in the description below so that one was about what to do in the catch blog with examples and today we'll be talking about laravel apis pretty typical scenario how API in laravel Returns the default error message which could be improved so have you seen this one if you have find or fail or if you have route model binding this would be the message that laravel would return no query results for model model path and ID number and this is quite bad in a few ways so the first it's not user friendly so non-technical user would not understand that if for example your frontender Returns the error message as it is without any filtering then it Returns the ID which may be a security issue if someone tries to guess the IDS and maybe do something harmful with that and and also it Returns the model class which tells the potential hacker as well that it uses laravel that it uses such and such model structure which is a data League which is in theory a security issue but even if you don't care that much about security let's say that it's not user friendly in the first place so how do we override that message so for example you have something like this so find or fail or you would have user user here as a parameter for Route model binding and you can override that without any try catch in the official documentation they say that you could go to Handler PHP which is by default in laravel and provide this this renderable if there is not found HTTP exception you just return whatever you want to return and don't forget the status code of 404 because status codes are really important in the apis but you can override the message to whatever you want but the problem with that approach is that it is global for all the models for all the cases of of not found HTTP exception but maybe you want to override the message only for specific controller API endpoint or for specific model so that resource not found would be Global but let's take a look if you want to perform something for specific model so here's where you would use try catch and with try user find or fail you could catch specific exception and provide that user is not found and you could also catch multiple exceptions so in case of model not file it's user not found but in case of any other exception you may even provide the error code of 500 which means this means not found kind of a validation error and 500 means something went really wrong on the server so this is where try catch is useful to overwrite the default behavior of this exception in the apis for specific controller and endpoint and this is an example of catching multiple exceptions for example from the package maybe use something for incomplete data or no access or something like that so there could be many catch blocks in one try statement so this is just a quick example how you could override the default API error from laravel and again if you want the full course the link will be in the description below this is a text based course about exceptions PHP errors with examples of DB transactions and other stuff that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 2,170
Rating: undefined out of 5
Keywords:
Id: Tdh4oCe0rlc
Channel Id: undefined
Length: 3min 26sec (206 seconds)
Published: Wed Jun 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.