9

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 Answers 4

26

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for helping! I'm almost there but I can't find out how to render a custom twig template. (I don't need to use twig, since it's not a long text, but I prefer using it)
twig configuration to stop rendering the default twig template.
Thank you for the help! I figured out a way to render specific twig template by editing the Exception Listener. link you gave me also helped!
6

I recently implemented a custom exception in my Symfony2 service in the following manner:

MemberAlreadyExistsException.php

<?php

namespace Aalaap\MyAppBundle\Services\Membership;

class MemberAlreadyExistsException extends \Exception
{

}

Subscriber.php

<?php

namespace Aalaap\MyAppBundle\Services\Membership;
...
throw new MemberAlreadyExistsException(
    'The member you are trying to subscribe already'
    . ' exists in this list.'
);
...

1 Comment

Quick and simple.
0

You have to extend the Exception class, or at least the Symfony's inner exception class

Comments

0

I just had to add \ and the global scope worked in a Symfony service

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.');
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.