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

查看:32
本文介绍了如何在 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.

我想要做的是创建一个自定义的错误异常侦听器,并在我的控制器和服务中使用它,

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 twig 模板.(我正在为我的原生智能手机应用程序开发一个后台程序)

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 并创建了一个自定义异常类,如以下链接所示(覆盖 Symfony 2 异常?) 但我收到错误

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:

首先创建您的自定义异常接口

first create your customExceptionInterface

namespace MySymfonyBundleException;
/**
 * Interface for my bundle exceptions.
 */
interface MySymfonyBundleExceptionInterface
{
}

并创建您的 jsonErrorException

namespace MySymfonyBundleException;

class HttpException extends Exception implements MySymfonyBundleExceptionInterface
{
}

不要犹豫,浏览symfony 的异常代码示例github

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

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

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