Laravel 4 中的异常处理 - 理解流程的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17071276/
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
Exception handling in Laravel 4 - Problems understanding flow
提问by grange
I have problems understanding how Laravel handles exceptions. I registered Exception handlers in global.php like this:
我在理解 Laravel 如何处理异常时遇到问题。我在 global.php 中注册了异常处理程序,如下所示:
use MyNamespace\Custom\Exceptions\NotAllowedException;
App::error(function(NotAllowedException $exception, $code)
{
die("MyNamespace\Custom\Exceptions\NotAllowedException catched");
});
App::error(function(\Exception $exception)
{
echo "general exception thrown<br/>";
});
In a controller action I now throw a NotAllowedException. The strange part however is, that first Exception is catched and NotFoundException afterwards.
在控制器操作中,我现在抛出 NotAllowedException。然而,奇怪的部分是,第一个 Exception 被捕获,然后 NotFoundException 被捕获。
The output thus is:
因此输出是:
general exception thrown
MyNamespace\Custom\Exceptions\NotAllowedException catched
I thought that exception handlers stack and therefore only NotAllowedException is being handled. But I'm wrong. Do I misunderstand the concepts of error handling in Laravel or is this unexpected behaviour?
我认为异常处理程序堆栈,因此只处理 NotAllowedException。但我错了。我是否误解了 Laravel 中错误处理的概念,或者这是意外行为?
Another thing: I am not able to set http response header to 401. There are other threads on SO regarding this problem but without a solution so far. If anyone knows something about that either, I would appreciate.
另一件事:我无法将 http 响应标头设置为 401。关于此问题,SO 上还有其他线程,但到目前为止还没有解决方案。如果有人对此有所了解,我将不胜感激。
Thank you for you time and every response! Cheers
感谢您的时间和每一个回复!干杯
回答by DerLola
Exception handling can be seen as a reversed waterfall. The last handler that is defined, is checked first. Take this example:
异常处理可以看作是一个反向瀑布。首先检查定义的最后一个处理程序。拿这个例子:
// Custom Exception
class CustomException extends Exception {}
// Error handler in global.php
App::error(function(Exception $exception, $code)
{
echo 'Debug: Exception<br/>';
});
App::error(function(CustomException $exception, $code)
{
echo 'Debug: CustomException<br/>';
});
// Exception in routes.php (or any other place)
throw new CustomException();
Both types match the Exception type, so this outputs: Debug: CustomException Debug: Exception
两种类型都匹配 Exception 类型,因此输出: Debug: CustomException Debug: Exception
However, if you return something from within your handler, ONLY the first matching handler is triggered. To return a JSON response with a HTTP 401 response code, do something like:
但是,如果您从处理程序中返回某些内容,则只会触发第一个匹配的处理程序。要返回带有 HTTP 401 响应代码的 JSON 响应,请执行以下操作:
App::error(function(Exception $exception, $code)
{
return Response::json(array(
'error' => 'Something went wrong (Exception)'
), 500);
});
App::error(function(NotAllowedException $exception, $code)
{
return Response::json(array(
'error' => 'Something went wrong (NotAllowedException)'
), 401);
});
So, in general, you'd want the Exception handler defined first.
因此,一般而言,您希望首先定义异常处理程序。