I'm handling laravel exceptions such as NotFoundHttpException and UnauthorizedException by type-checking exceptions in the exception handler's render method, making a lot of instanceof checks which in my opinion violates OCP (open for extension closed for modification) principle. The documentaion solves this by using renderable exceptions (https://laravel.com/docs/6.x/errors#renderable-exceptions) that must be thrown from the controller (or any where) which I don't want, I want to catch laravel exceptions' such as ModelNotFoundException in the handler class and return my custom response in a clean way,in other words, I'm looking for a clean way to handle laravel exceptions without throwing the exception from the controller.
1
There are 1 best solutions below
Related Questions in LARAVEL
- Function in anonymous Laravel Blade component
- Composer installation fails and reverts ./composer.json and ./composer.lock to original content
- Laravel: Using belongsToMany relationship with MongoDB
- Laravel's whereBetween method not working with two timestamps
- Implementing UUID as primary key in Laravel intermediate table
- Resolving ElephantIO ServerConnectionFailureException: Error establishing connection to server
- Undefined function getAdminPanelUrlPrefix()'error in Laravel SaaS project after installing chatmessenger
- PHP Laravel SQLServer could not find driver
- Laravel installation via Composer results in connection timeout error
- Is there a way of showing content in a Statamic antlers template if a user is authenticated?
- Livewire component JS script Uncaught SyntaxError: Unexpected token
- is there a solution to run cron job command in cpanel only from my cPanel host?
- Prevent a webpage from navigating away
- Deploy Flutter and Laravel php mobile app on the host server
- Please how I fetch user account balance, withdrawals, Loans and Transactions to display in the dashboard?
Related Questions in EXCEPTION
- What should i use Exceptions or Monads for handle if service occur a problem?
- Python Requests: Handling Exceptions and Ensuring Server Response
- What is a better way to allow no user input while also preventing non-number inputs
- New error on random number assigned to local variable , Rails
- spring error exception with oauth2 and securityconfig
- Exception thrown: 'System.InvalidOperationException' in Microsoft.Data.SqlClient.dll
- How to fix this Row nested in Column exception issue in Flutter?
- GDI - Why the printing StartPage() function works in 32 bit but raises an exception in 64 bit?
- Handling Invalid Credential or Login error Exception in Python for Network Devices
- Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1Error:
- .NET 6 Custom Nuget package referencing other packages - Do I have to include the other packages myself?
- How to prevent Unity from catching and ignoring ALL exceptions
- My Google Apps Script renames all files in a folder from data in a spreadsheet. Can someone explain why it returns an exception error?
- Python (pylint): Catching general exceptions in validation procedures
- Need a simple example how to catch a data type error en C++
Related Questions in LARAVEL-EXCEPTIONS
- How to use render( method of custom exception?
- Have I to define custom exception class ItemValidAccess in mehod?
- Understanding Laravel exception handling catching exceptions in the controller that have been thrown in service layer
- How to make custom exception in http unit test?
- custom exception of Laravel is not working as expected
- Why I failed to catch ModelNotFoundException ths error in controller block?
- How to catch custom error raised in method of object?
- how to add exception in laravel migration?
- How to checks raised exceptions in phpunit tests?
- Laravel - Seeing Symfony Exception page instead of Spatie Ignition
- Manually throw Laravel ValidationException VS FormRequestValidationException
- Laravel Error in Logs Doesn't Tell Me What I Need to Fix
- How may I call a blade view in a custom Http Exception? (Laravel 7)
- Laravel handle PDO Exception message
- Handle exceptions in laravel without type-checking exceptions in the exception handler's render method and without defining custom exception?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
If it is about OCP only, you can use a chain of responsibility design pattern, you can create a class (which is open-for-extension closed-for-modifications), that has a "next" field, which is from the same class type, and make the conversions you want like that:
Then implement it as follows:
So, now you check on
ModelNotFoundExceptiononly, if you want to check on other types, create theModelNotFoundNodewith$nextnot equal to null, but rather equal to for exampleValidationException(you would implement it the same way), so adding any other exception of those would be just making another class that extendsNodeInErrorHandlerChain, and make it the$nextto the previous last element of the chain, when you create the chain (for example in the Provider which provides dependency injection in your app).