如何在symfony2上添加自定义异常 [英] How to add a custom exception on symfony2

查看:94
本文介绍了如何在symfony2上添加自定义异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Symfony的新手(使用2.2版),并尝试添加自定义异常侦听器. 我已阅读以下链接,但无法正常工作.

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.

  • 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');

显示这样的json树枝模板.(我正在为我的本机智能手机应用程序开发一个后台程序)

to display json twig template like this.(I'm developing a background program for my native smartphone applications)

{"status": "error", "message": "invalid_params"}

我已经将CustomEventListener注册到src/My/Bundle/Resources/config/services.yml,并创建了一个自定义异常类,如以下链接所示(

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

symfony exceptions must be valid objects derived from the exception base class

我在这里做错什么了吗?谢谢.

Am I doing something wrong here? Thanks.

推荐答案

您可以使用"symfony方式"创建自定义异常,让我们看看如何在symfony中创建异常或创建

You can create custom exception the "symfony way" let's look at how exception or created in symfony:

首先创建您的customExceptionInterface

first create your customExceptionInterface

namespace My\SymfonyBundle\Exception;
/**
 * Interface for my bundle exceptions.
 */
interface MySymfonyBundleExceptionInterface
{
}

并创建您的jsonErrorException

namespace My\SymfonyBundle\Exception;

class HttpException extends \Exception implements MySymfonyBundleExceptionInterface
{
}

请随时浏览 symfony的异常代码示例github

Don't hesitate to browse symfony's exceptions code examples on github

这篇关于如何在symfony2上添加自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆