php 如何在 symfony2 上添加自定义异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16099962/
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 add a custom exception on symfony2
提问by Kyosuke Nakajima
I am new to Symfony(using version 2.2) and trying to add a custom exception listener. I have read the following links but cannot get it to work.
我是 Symfony 的新手(使用 2.2 版)并尝试添加自定义异常侦听器。我已阅读以下链接,但无法使其正常工作。
- Overriding Symfony 2 exceptions?
- Symfony2 Custom Error Exception Listener - Rendering templates or passing to a controller
What I'm trying to do is to create a custom Error Exception Listener and use it from my controllers and services like this,
我想要做的是创建一个自定义的错误异常侦听器,并从我的控制器和服务中使用它,如下所示,
throw new jsonErrorException('invalid_params');
to display json twig template like this.(I'm developing a background program for my native smartphone applications)
像这样显示 json twig 模板。(我正在为我的本机智能手机应用程序开发后台程序)
{"status": "error", "message": "invalid_params"}
I have registered my CustomEventListener to my src/My/Bundle/Resources/config/services.yml and created a custom exception class as shown on following link(Overriding Symfony 2 exceptions?) but I get the error
我已将我的 CustomEventListener 注册到我的 src/My/Bundle/Resources/config/services.yml 并创建了一个自定义异常类,如下面的链接所示(覆盖 Symfony 2 异常?)但我收到错误
symfony exceptions must be valid objects derived from the exception base class
Am I doing something wrong here? Thanks.
我在这里做错了吗?谢谢。
回答by Paul Valla
You can create custom exception the "symfony way" let's look at how exception or created in symfony:
您可以通过“symfony 方式”创建自定义异常,让我们看看异常是如何在 symfony 中创建的:
first create your customExceptionInterface
首先创建您的 customExceptionInterface
namespace My\SymfonyBundle\Exception;
/**
* Interface for my bundle exceptions.
*/
interface MySymfonyBundleExceptionInterface
{
}
And create your jsonErrorException
并创建您的 jsonErrorException
namespace My\SymfonyBundle\Exception;
class HttpException extends \Exception implements MySymfonyBundleExceptionInterface
{
}
Don't hesitate to browse symfony's exceptionscode examples on github
不要犹豫,在 github 上浏览symfony 的异常代码示例
回答by aalaap
I recently implemented a custom exception in my Symfony2 service in the following manner:
我最近通过以下方式在我的 Symfony2 服务中实现了一个自定义异常:
MemberAlreadyExistsException.php
MemberAlreadyExistsException.php
<?php
namespace Aalaap\MyAppBundle\Services\Membership;
class MemberAlreadyExistsException extends \Exception
{
}
Subscriber.php
Subscriber.php
<?php
namespace Aalaap\MyAppBundle\Services\Membership;
...
throw new MemberAlreadyExistsException(
'The member you are trying to subscribe already'
. ' exists in this list.'
);
...
回答by michalzuber
I just had to add \
and the global scope worked in a Symfony service
我只需要添加\
和全局范围在 Symfony 服务中工作
namespace App\CoreBundle\Service;
class CurrencyExchange
{
const RATES_XML = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?849b4b329863b2d60bfd0de486e423c9';
const RATES_XML_PATH = 'uploads/ecb_currencies.xml';
/** @var array $rates */
private $rates;
public function __construct()
{
if (!is_file(self::RATES_XML_PATH)) {
throw new \Exception('XML '.self::RATES_XML_PATH.' does not exists.');
}
if (1 > filesize(self::RATES_XML_PATH)) {
throw new \Exception('XML '.self::RATES_XML_PATH.' is empty.');
}
回答by Royal Bg
You have to extend the Exception class, or at least the Symfony's inner exception class
您必须扩展 Exception 类,或者至少扩展 Symfony 的内部异常类