cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Default error handler

Open niden opened this issue 14 years ago • 5 comments

Error handler - it would be cool if instead of try catch block I could define where my exceptions should be routed to and where I could display a proper 404/500 page to the user. It could be done by set_exception_handle but it would be even cooler if Phalcon would handle it internally.

i.e. default route /error/index

niden avatar Sep 09 '12 23:09 niden

Feedback by Nesbert in the google groups:

+1 for Error handler... in the interim here is an example of how am dealing with issue https://gist.github.com/3701633#L113

niden avatar Sep 11 '12 20:09 niden

Another +1 for this. With options to configure per module too, set error handler to module, controller, action and pass in exception object or something?

brandonlamb avatar Sep 19 '12 01:09 brandonlamb

+1 for error handler. This is not normal 404 response for production website: http://phalconphp.com/indexxxx And i can't figure out where and how i can add internal forward like: $app->dispatcher->forward(array("controller" => "index", "action" => "error404"));

johnnyajax avatar Oct 17 '12 00:10 johnnyajax

Right now the main script of the website as it can be seen in github encapsulates operations with a try catch.

When an exception happens it only prints out the error message from the exception.

Working a bit on the try catch code could trap and display things better until this feature is developed. :) On Oct 16, 2012 8:07 PM, "JohnnyAjax" [email protected] wrote:

+1 for error handler. This is not normal 404 response for production website: http://phalconphp.com/indexxxx

— Reply to this email directly or view it on GitHubhttps://github.com/phalcon/cphalcon/issues/66#issuecomment-9511773.

niden avatar Oct 17 '12 00:10 niden

0.6.0 now supports an error handler for exceptions produced by the dispatcher

$di->set('dispatcher', function(){

    //Create/Get an EventManager
    $eventsManager = new Phalcon\Events\Manager();

    //Attach a listener
    $eventsManager->attach("dispatch", function($event, $dispatcher, $exception) {

        //The controller exists but the action not
        if ($event->getType() == 'beforeNotFoundAction') {
            $dispatcher->forward(array(
                'controller' => 'index',
                'action' => 'show404'
            ));
            return false;
        }

        //Alternative way, controller or action doesn't exists
        if ($event->getType() == 'beforeException') {
            switch ($exception->getCode()){
                case Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(array(
                        'controller' => 'index',
                        'action' => 'show404'
                    ));
                    return false;               
            }
        }
    });

    $dispatcher = new Phalcon\Mvc\Dispatcher();

    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

ghost avatar Oct 19 '12 17:10 ghost