dsf12313 2013-04-19 07:57
浏览 63
已采纳

如何在symfony2上添加自定义异常

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.

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)

{"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

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

Am I doing something wrong here? Thanks.

  • 写回答

4条回答 默认 最新

  • du4373 2013-04-19 08:38
    关注

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

    first create your customExceptionInterface

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

    And create your jsonErrorException

    namespace My\SymfonyBundle\Exception;
    
    class HttpException extends \Exception implements MySymfonyBundleExceptionInterface
    {
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧