-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[BUG] v1.3.0 - 'dispatch:beforeException' of Phalcon\Mvc\Dispatcher never fires when an Exception is thrown. #1763
Comments
If we call beforeException and abort the request, will this be acceptable? |
$response = $app->handle();
foreach ($app->getConfig()['params']['log_devices'] as $device => $config) {
if ($app->getDI()->getService($device)->isResolved()) {
/**
* @var Phalcon\Logger\Adapter
*/
$device = $app->getDI()->get($device);
if ($device->isTransaction()) {
$device->commit();
}
}
}
if (isset($response)) {
echo $response->getContent();
} are you offering to fire |
Don't think so — I did not look back far away into the past but it looks like beforeException was called only in two cases:
Not exactly. If an event handler throws an exception, we will call beforeException and exit the dispatch loop, just like as if the handler returned |
Yeah, that's fine. As long as I can capture thrown Exception and log it and then reach the end of index.php - I'm happy :-) Thanks! |
OK, I modified index,php as follows: diff --git a/simple/public/index.php b/simple/public/index.php
index b1f97e0..68365c4 100644
--- a/simple/public/index.php
+++ b/simple/public/index.php
@@ -16,7 +16,22 @@ $di = new \Phalcon\DI();
$di->set('router', 'Phalcon\Mvc\Router');
//Registering a dispatcher
-$di->set('dispatcher', 'Phalcon\Mvc\Dispatcher');
+$di->set('dispatcher', function() {
+ $eventsManager = new Phalcon\Events\Manager;
+
+ $eventsManager->attach('dispatch:beforeDispatchLoop', function(Phalcon\Events\Event $event, Phalcon\Mvc\Dispatcher $dispatcher) {
+ throw new Exception('Just because.');
+ });
+
+ $eventsManager->attach('dispatch:beforeException', function($event, $dispatcher, $exception) {
+ error_log('dispatch:beforeException' . PHP_EOL);
+ });
+
+ $dispatcher = new Phalcon\Mvc\Dispatcher;
+ $dispatcher->setEventsManager($eventsManager);
+
+ return $dispatcher;
+});
//Registering a Http\Response
$di->set('response', 'Phalcon\Http\Response');
@@ -50,6 +65,7 @@ try {
$application = new \Phalcon\Mvc\Application();
$application->setDI($di);
echo $application->handle()->getContent();
+ echo 'After $application->handle()->getContent()', PHP_EOL;
}
catch(Phalcon\Exception $e){
echo $e->getMessage(); The updated Phalcon is here if you want to test it: https://github.com/sjinks/cphalcon/tree/issue-1763 (I will submit a pull request later because I need to write a test first and it is late night / early morning here 😄) The output of the script:
which looks good to me |
Thanks! It's getting late here too (GMT-5), I will check it out tomorrow morning. |
I've just compiled latest 1.3.0 and this is not working for me: $di = new \Phalcon\DI\FactoryDefault();
$di->set('dispatcher', function() {
$eventsManager = new Phalcon\Events\Manager;
$eventsManager->attach('dispatch:beforeDispatchLoop', function($event, $dispatcher) {
throw new Exception('Just because.');
});
$eventsManager->attach('dispatch:beforeException', function($event, $dispatcher, $exception) {
while (ob_get_level()) {
ob_end_clean();
}
echo 'dispatch:beforeException', PHP_EOL;
ob_start();
});
$dispatcher = new Phalcon\Mvc\Dispatcher;
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
$di->set('view', 'Phalcon\\Mvc\\View');
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
echo 'After $application->handle()->getContent()', PHP_EOL; Result: <b>Fatal error</b>: Uncaught exception 'Exception' with message 'Just because.' in /var/web/nginx/html/public/e.php:8
Stack trace:
#0 [internal function]: {closure}(Object(Phalcon\Events\Event), Object(Phalcon\Mvc\Dispatcher), NULL)
#1 [internal function]: Phalcon\Events\Manager->fireQueue(Array, Object(Phalcon\Events\Event))
#2 [internal function]: Phalcon\Events\Manager->fire('dispatch:before...', Object(Phalcon\Mvc\Dispatcher))
#3 [internal function]: Phalcon\Dispatcher->dispatch()
#4 /var/web/nginx/html/public/e.php(29): Phalcon\Mvc\Application->handle()
#5 {main}
thrown in <b>/var/web/nginx/html/public/e.php</b> on line <b>8</b><br />
Phalcon was compiled using build/install script. |
Please either build from ext/ or regenerate the files in build/ with |
yea, that was it. my exception logger is back to normal, thanks a lot. could you please explain this ext/ rebuild business to me? why/when do I have to do it, when do fixes actually go into build/ ? what's the methodology. |
Normally all bug fixes are committed to ext/ since this is what we use during development. There is a special script, We (developers) run this script either before the release or when we change something drastically and want to make sure that the files in build/ can still be compiled. Thus, to be on the safe side, I would recommend you to run When Phalcon build from ext/ it is usually a bit slower than Phalcon build from build/ but the performance penalty is really hard to notice on the developer machine :-) |
thank you so much for the explanation. can I get your attention to #1078 - please see my latest comment. |
It looks like #1624 is now back. Can you please see if it could be somehow related to this bugfix? Thanks! |
No, not related |
What's about the exception handling with the event manager ? |
Latest Phalcon v.1.3.0.
Steps to reproduce:
index.php
, use the following:An Exception is thrown in
dispatch:beforeDispatchLoop
, I expect that I'd be able to capture it indispatch:beforeException
phase.This had worked before, I've been doing Exception logging for some time.
Thanks!
The text was updated successfully, but these errors were encountered: