Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0.10] dispatch:beforeException not executing when exception is thrown in dispatch process #11458

Closed
rkeplin opened this issue Feb 26, 2016 · 1 comment

Comments

@rkeplin
Copy link
Contributor

rkeplin commented Feb 26, 2016

When an exception is thrown in a dispatch loop event, beforeExecuteRoute for instance, beforeException never gets fired off.

This had been working in Phalcon 1.3.5, until I recently updated to Phalcon 2.0.10. Not sure if the desired functionality has changed, or if this is a bug. But I had found it useful when beforeException was fired off whenever an exception was thrown in a controller or other dispatch loop event.

Reproducible Test

<?php
use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Events\Event;

class IndexController extends Controller
{
    public function indexAction() {
        echo 'This is a test.';
    }
}

class AuthPlugin
{
    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher, $exception)
    {
        throw new Exception('Catch me in ExceptionPlugin, please!');
    }
}

class ExceptionPlugin
{
    public function beforeException(Event $event, Dispatcher $dispatcher, $exception)
    {
        echo 'I should end up here first.';
    }
}

$di  = new FactoryDefault();

$di->set('dispatcher', function() use($di) {
    $dispatcher = new Dispatcher();

    $eventsManager = $di->getShared('eventsManager');
    $eventsManager->attach('dispatch', new AuthPlugin());
    $eventsManager->attach('dispatch', new ExceptionPlugin());

    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
}, true);

$di->set('view', function () {
    $view = new View();

    return $view;
}, true);

$app = new Application();
$app->setDi($di);

try {
    echo $app->handle()->getContent();
} catch (Exception $e) {
    echo 'I should end up here second.';
}

Expected Result
I should end up here first. I should end up here second.

Observed Result
I should end up here second.

@rkeplin rkeplin changed the title [2.0.10] beforeException not executing when exception is thrown in dispatch loop events [2.0.10] dispatch:beforeException not executing when exception is thrown in dispatch process Feb 29, 2016
@sergeyklay
Copy link
Contributor

Fixed in 2.1.x branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants