如何绕过 Laravel 异常处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19360215/
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
How to bypass Laravel Exception handling
提问by Martijn
I have a method that checks if a user has valid Session info. This is supposed to throw an Exception, Guzzle\Http\Exception\BadResponseException
but when I try to catch it :
我有一种方法可以检查用户是否具有有效的会话信息。这应该抛出异常,Guzzle\Http\Exception\BadResponseException
但是当我尝试捕获它时:
catch (Guzzle\Http\Exception\BadResponseException $e)
{
return false;
}
return true
Laravel doesn't get to this code and immediately starts it's own error handling. And ideas on how to bypass Laravels own implementation and use my own Catch.
Laravel 没有得到这段代码并立即开始它自己的错误处理。以及如何绕过 Laravel 自己的实现并使用我自己的 Catch 的想法。
EDIT: I just found out Laravel uses the same Exception handler as Symfony, so I also added the Symfony2 tag.
编辑:我刚刚发现 Laravel 使用与 Symfony 相同的异常处理程序,所以我还添加了 Symfony2 标签。
EDIT 2:
编辑2:
I sort of fixed the issue by disabling Guzzle exceptions and checking the return header manually. It's a bit of a short cut but in this case, it does the job. Thanks for the responses!
我通过禁用 Guzzle 异常并手动检查返回标头来解决这个问题。这有点捷径,但在这种情况下,它可以完成工作。感谢您的回复!
回答by Antonio Carlos Ribeiro
Actually this exception can be catched in Laravel, you just have to respect (and understand) namespacing:
实际上这个异常可以在 Laravel 中捕捉到,你只需要尊重(并理解)命名空间:
If you have
如果你有
namespace App;
and you do
你也是
catch (Guzzle\Http\Exception\BadResponseException $e)
PHP understands that you are trying to
PHP 了解您正在尝试
catch (\App\Guzzle\Http\Exception\BadResponseException $e)
So, for it to work you just need a root slash:
所以,为了让它工作,你只需要一个根斜杠:
catch (\Guzzle\Http\Exception\BadResponseException $e)
And it will work.
它会起作用。
回答by The Alpha
By default, the app/start/global.php
file contains an error handler for all exceptions. However, you may specify more handlers if needed. Handlers are called based on the type-hint
of the Exception they handle. For example, you may create a handler that only handles your BadResponseException
instances, like
默认情况下,该app/start/global.php
文件包含所有异常的错误处理程序。但是,如果需要,您可以指定更多处理程序。处理程序根据type-hint
它们处理的异常进行调用。例如,您可以创建一个仅处理您的BadResponseException
实例的处理程序,例如
App::error(function(Guzzle\Http\Exception\BadResponseException $exception)
{
// Handle the exception...
return Response::make('Error! ' . $exception->getCode());
});
Also, make sure you have a well defined (BadResponseException
) class. Read more on Laravel Documentation.
另外,请确保您有一个明确定义的 ( BadResponseException
) 类。阅读更多关于Laravel 文档。
回答by AnDus
Instead of your code
而不是你的代码
catch (Guzzle\Http\Exception\BadResponseException $e)
{
return false;
}
return true
use this solution
使用此解决方案
catch (\Exception $e)
{
return false;
}
return true
to catch all possible exceptions thrown by Guzzle.
捕获 Guzzle 抛出的所有可能的异常。
If you explicitly want to catch a BadResponseExceptionyou can also prepend your exception's class namespace with '\'.
如果您明确想要捕获BadResponseException,您还可以在您的异常的类命名空间前加上'\'。
catch (\Guzzle\Http\Exception\BadResponseException $e)
{
return false;
}
return true