Laravel 5.5 custom exception handling

Core-i9

I'm using Laravel 5.5 and am new to it. As concerns error handling, I found that Laravel has many facades and features built-in, however, I seem not to have wrapped my head around all the features it provides (to originally make my life easier).

What I want is that whenever an error occurs (i.e. exception's thrown), Laravel shall redirect the user to the previous page and display an error message (not the error itself, you don't want users to be able to see this in production; I thought of something like 'Error occurred').

I included a display for errors in all of my views, so I just need to pass (via POST I suppose) an array message with title and message, as well as style (using Bootstrap styles, e.g. danger, warning, success). Ideally, the exception is logged somewhere so I'm able to reproduce errors later on.

My current solution is devoid of Laravel's nice features since I just try-catch everywhere and redirect to a specific page (I've chosen to be 'the right one' for this error). Also, this oppresses the original error.

What is the best approach for my desire?

jedrzej.kurylo

Have a look at Laravel's exception handler: https://laravel.com/docs/5.5/errors#the-exception-handler

You can achieve what you want by implementing custom render() method that would return a redirect response.

The render method is responsible for converting a given exception into an HTTP response that should be sent back to the browser.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事