2009年1月29日星期四

[fw-mvc] Throwing an exception in the Router

Hi everyone,

The design of Zend_Controller_Front::dispatch() has two levels of try/catch and handles exceptions thrown in routing separately from exceptions thrown during dispatch.  This means that exceptions thrown in the router will not be picked up by Zend_Controller_Plugin_ErrorHandler because the entire dispach loop never runs.

It seems valid to be able to throw exceptions during routing; in fact, the API recommends throwing a Zend_Controller_Router_Exception.  But am I overlooking something?  Is there a design reason why dispatching is skipped during this case, or was it just overlooked? 

If there's a better way to handle exceptions during routing then please let me know, otherwise I have a working patch that corrects this behaviour (tested against ZF 1.7.3).  It essentially wraps another try/catch around the routing functions.  Replace lines 893 - 916 in Zend_Controller_Front with the following:

try {
            /**
            * Notify plugins of router startup
            */
            $this->_plugins->routeStartup($this->_request);

            $router->route($this->_request);

            /**
            * Notify plugins of router completion
            */
            $this->_plugins->routeShutdown($this->_request);

            /**
            * Notify plugins of dispatch loop startup
            */
            $this->_plugins->dispatchLoopStartup($this->_request);

        } catch (Exception $e) {
            if ($this->throwExceptions()) {
                throw $e;
                }
            $this->_response->setException($e);
        }

Thanks to everyone for their work on the framework!

Cheers, Scott

                                                                                                                 
Scott Davey » Datalink: creative e-business
www.datalink.com.au | 1300.DATALINK | Melbourne, Australia



没有评论: