Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
327 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

And create your jsonErrorException

namespace MySymfonyBundleException;

class HttpException extends Exception implements MySymfonyBundleExceptionInterface
{
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...