MVC Exceptions
Introduction
The MVC components in Zend Framework utilize a Front Controller,
which means that all requests to a given site will go through a
single entry point. As a result, all exceptions bubble up to the
Front Controller eventually, allowing the developer to handle them
in a single location.
However, exception messages and backtrace information often contain
sensitive system information, such as SQL statements, file
locations, and more. To help protect your site, by default
Zend_Controller_Front catches all exceptions and
registers them with the response object; in turn, by default, the
response object does not display exception messages.
Handling Exceptions
Several mechanisms are built in to the MVC components already to
allow you to handle exceptions.
-
By default, the error
handler plugin is registered and active. This plugin
was designed to handle:
It operates as a postDispatch() plugin, and
checks to see if a dispatcher, action controller, or
other exception has occurred. If so, it forwards to an error
handler controller.
This handler will cover most exceptional situations, and
handle missing controllers and actions gracefully.
-
Zend_Controller_Front::throwExceptions()
By passing a boolean TRUE value to this method, you can
tell the front controller that instead of aggregating exceptions
in the response object or using the error handler plugin,
you'd rather handle them yourself. As an example:
span style="color: #808080; font-style: italic;">// handle exceptions yourself
}
This method is probably the easiest way to add custom
exception handling covering the full range of possible
exceptions to your front controller application.
-
Zend_Controller_Response_Abstract::renderExceptions()
By passing a boolean TRUE value to this method, you tell
the response object that it should render an exception message
and backtrace when rendering itself. In this scenario, any
exception raised by your application will be displayed. This
is only recommended for non-production environments.
-
Zend_Controller_Front::returnResponse() and
Zend_Controller_Response_Abstract::isException().
By passing a boolean TRUE to
Zend_Controller_Front::returnResponse(),
Zend_Controller_Front::dispatch() will not render the
response, but instead return it. Once you have the response,
you may then test to see if any exceptions were trapped using
its isException() method, and retrieving the
exceptions via the getException() method. As an
example:
span style="color: #808080; font-style: italic;">// handle exceptions ...
The primary advantage this method offers over
Zend_Controller_Front::throwExceptions() is to
allow you to conditionally render the response after
handling the exception. This will catch any exception in the
controller chain, unlike the error handler plugin.
MVC Exceptions You May Encounter
The various MVC components -- request, router, dispatcher, action
controller, and response objects -- may each throw exceptions on
occasion. Some exceptions may be conditionally overridden, and
others are used to indicate the developer may need to consider
their application structure.
As some examples:
|
|