symfony2中的自定义异常行为 [英] custom exception behavior in symfony2
问题描述
抛出新的\Exception(您的请求出错,请重试)抛出异常;
我自动获取状态500,消息作为内部服务器错误
但是,我反而喜欢我的回复包括我的异常消息,而不是内部服务器错误,因此它显示如下所示:
{
error:{
code:500,
message:您的请求发生错误,请再试一次
}
}
除此之外,还可能会做一些额外的事情,例如给我自己发送错误。但是,我只希望这样会发生在我抛出一个\Exception而不是使用像
$ pre>
throw new HttpException
有关如何完成此任务的任何帮助或想法。
我还应该提到,我没有使用Twig或任何模板。这是严格的API类型响应
看看 http://symfony.com/doc/current/cookbook/controller/error_pages.html 有足够的信息让你开始。
$ b简而言之,您应该创建应用程序/ Resources / TwigBundle / views / Exception / exception.json.twig,并且您可以访问exception.message和error_code。这里是您的解决方案:
{%spaceless%}
{
error:{
code:{{error_code}},
message:{{exception.message}}
}
}
{ %endspaceless%}
另一个解决方案是使用异常监听器:
命名空间Your\Namespace;
使用Symfony\Component\HttpFoundation\JsonResponse;
使用Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class JsonExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $ event)
{
$ exception = $ event-> getException();
$ data = array(
'error'=>数组(
'code'=> $ exception-> getCode(),
'message'=& $ exception-> getMessage()
)
);
$ response = new JsonResponse($ data);
$ event-> setResponse($ response);
}
}
更新您的服务配置:
json_exception_listener:
class:Your\Namespace\JsonExceptionListener
标签:
- {name:kernel.event_listener ,event:kernel.exception,方法:onKernelException,priority:200}
cheers
Im trying to figure out how to make a custom exception behavior. When i throw a exception using
throw new \Exception('Error occurred with your request please try again');
I automatically get status 500 and the message as internal server error
However i would instead like my response to include my exception message instead of just internal server error so for it to display something like so:
{
"error":{
"code":500,
"message":"Error occurred with your request please try again"
}
}
and on top of that possibly do some extra things such as email myself the error. However I only want this to happen when i throw a \Exception as opposed to using something like
throw new HttpException
Any help or ideas on how to accomplish this.
I should also mention that I am not using Twig or any templates for this. This is strictly a API type response
Take a look at http://symfony.com/doc/current/cookbook/controller/error_pages.html There is enough information to get you started.
In short, you should create app/Resources/TwigBundle/views/Exception/exception.json.twig and there you have access to the exception.message and error_code.
here's solution for you:
{% spaceless %}
{
"error":{
"code": {{ error_code }},
"message":{{ exception.message }}
}
}
{% endspaceless %}
Another solution is to use Exception Listener:
namespace Your\Namespace;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class JsonExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$data = array(
'error' => array(
'code' => $exception->getCode(),
'message' => $exception->getMessage()
)
);
$response = new JsonResponse($data);
$event->setResponse($response);
}
}
update your services config:
json_exception_listener:
class: Your\Namespace\JsonExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 200 }
cheers
这篇关于symfony2中的自定义异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!