Catching Exception thrown in _forward

3 messages Options
Embed this post
Permalink
Cadrach

Catching Exception thrown in _forward

Reply Threaded More More options
Print post
Permalink
Hi everyone,

I have the following issue, inside an action I would like to catch the Exception thrown inside a $this->_forward I am making.

Example code of my controller:
    public function initAction()
    {
        $this->getFrontController()->setParam('noErrorHandler', true);
        $this->getFrontController()->throwExceptions(true);
        try
        {
            $this->_forward('test');
        }
        catch(Exception $e)
        {
            echo "CAUGHT";
            exit;
        }
    }

    public function testAction()
    {
        throw new Exception('TEST');
    }

This code displays the exception using standard PHP display, but the "catch" is never run.

Any idea what I am doing wrong? Or is this the excepted behavior of _forward()?

Thanks for reading,
Cadrach
Hector Virgen

Re: Catching Exception thrown in _forward

Reply Threaded More More options
Print post
Permalink
From what I understand, _forward only modifies the request and resets its dispatched flag. In other words, you can't catch any exception thrown in the action because the action isn't actually dispatched at that point.

Generally, exceptions thrown by actions are caught in the ErrorHandler plugin and forwarded to the ErrorController. Can you handle your error in the ErrorController?

--
Hector


On Wed, Oct 28, 2009 at 10:26 AM, Cadrach <[hidden email]> wrote:

Hi everyone,

I have the following issue, inside an action I would like to catch the
Exception thrown inside a $this->_forward I am making.

Example code of my controller:
   public function initAction()
   {
       $this->getFrontController()->setParam('noErrorHandler', true);
       $this->getFrontController()->throwExceptions(true);
       try
       {
           $this->_forward('test');
       }
       catch(Exception $e)
       {
           echo "CAUGHT";
           exit;
       }
   }

   public function testAction()
   {
       throw new Exception('TEST');
   }

This code displays the exception using standard PHP display, but the "catch"
is never run.

Any idea what I am doing wrong? Or is this the excepted behavior of
_forward()?

Thanks for reading,
Cadrach
--
View this message in context: http://www.nabble.com/Catching-Exception-thrown-in-_forward-tp26098718p26098718.html
Sent from the Zend MVC mailing list archive at Nabble.com.


Cadrach

Re: Catching Exception thrown in _forward

Reply Threaded More More options
Print post
Permalink
Thanks, it helped me find a workaround. Here is what I did:

    public function initAction()
    {
        $this->getFrontController()->getPlugin('Zend_Controller_Plugin_ErrorHandler')
        ->setErrorHandlerModule('somemodule')
        ->setErrorHandlerController('somecontroller')
        ->setErrorHandlerAction('someaction');

        $this->_forward('somewhere');
       
    }

In the Controller somecontroller of Module somemodule:

    public function someactionAction()
    {
        $error = $this->_getParam('error_handler');

        // Then work using $error->exception as my exception
    }

Hector Virgen wrote:
From what I understand, _forward only modifies the request and resets its
dispatched flag. In other words, you can't catch any exception thrown in the
action because the action isn't actually dispatched at that point.

Generally, exceptions thrown by actions are caught in the ErrorHandler
plugin and forwarded to the ErrorController. Can you handle your error in
the ErrorController?

--
Hector


On Wed, Oct 28, 2009 at 10:26 AM, Cadrach <masterachid@yahoo.com> wrote:

>
> Hi everyone,
>
> I have the following issue, inside an action I would like to catch the
> Exception thrown inside a $this->_forward I am making.
>
> Example code of my controller:
>    public function initAction()
>    {
>        $this->getFrontController()->setParam('noErrorHandler', true);
>        $this->getFrontController()->throwExceptions(true);
>        try
>        {
>            $this->_forward('test');
>        }
>        catch(Exception $e)
>        {
>            echo "CAUGHT";
>            exit;
>        }
>    }
>
>    public function testAction()
>    {
>        throw new Exception('TEST');
>    }
>
> This code displays the exception using standard PHP display, but the
> "catch"
> is never run.
>
> Any idea what I am doing wrong? Or is this the excepted behavior of
> _forward()?
>
> Thanks for reading,
> Cadrach
> --
> View this message in context:
> http://www.nabble.com/Catching-Exception-thrown-in-_forward-tp26098718p26098718.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
>
>