Laravel 5.5 自定义异常处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47237910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.5 custom exception handling
提问by Core-i9
I'm using Laravel 5.5and 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).
我正在使用Laravel 5.5并且是新手。关于错误处理,我发现 Laravel 有许多内建的外观和功能,但是,我似乎并没有考虑到它提供的所有功能(最初是为了让我的生活更轻松)。
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').
我想要的是,无论何时发生错误(即抛出异常),Laravel 都会将用户重定向到上一页并显示错误消息(不是错误本身,您不希望用户能够在生产中看到这一点;我想到了类似的东西'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 messagewith titleand 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.
我在我的所有视图中都包含了一个错误显示,所以我只需要传递(我想是通过 POST)一个message带有titleand的数组message,以及style(使用 Bootstrap 样式,例如danger, warning, success)。理想情况下,异常记录在某处,以便我以后能够重现错误。
My current solution is devoid of Laravel's nice features since I just try-catcheverywhere and redirect to a specific page (I've chosen to be 'the right one' for this error). Also, this oppresses the original error.
我当前的解决方案缺乏 Laravel 的好功能,因为我try-catch到处都是并重定向到特定页面(我已选择成为此错误的“正确页面”)。此外,这压制了原始错误。
What is the best approach for my desire?
满足我的愿望的最佳方法是什么?
采纳答案by jedrzej.kurylo
Have a look at Laravel's exception handler: https://laravel.com/docs/5.5/errors#the-exception-handler
看看 Laravel 的异常处理程序: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.
您可以通过实现render()返回重定向响应的自定义方法来实现您想要的。
The
rendermethod is responsible for converting a given exception into an HTTP response that should be sent back to the browser.
该
render方法负责将给定的异常转换为应发送回浏览器的 HTTP 响应。
回答by Core-i9
This is my solution which excludes the validation and thus supports Laravel's built-in validation error, whereas every other error is differently displayed:
这是我的解决方案,它排除了验证,因此支持 Laravel 的内置验证错误,而其他每个错误的显示方式都不同:
Log::error($exception->getMessage());
if($exception instanceof \Illuminate\Validation\ValidationException)
return parent::render($request, $exception);
return redirect(URL::previous())->withErrors(['Error', 'Unknown Error']);
Thanks to @jedrzej.kurylo who posted a solution on where to start. The code above shall be used under /App/Exceptions/Handler.php, function render.
感谢@jedrzej.kurylo,他发布了一个关于从哪里开始的解决方案。上面的代码应该在/App/Exceptions/Handler.php, function下使用render。

