Studyon Minte9.com
ZF 1.0




Exception



Exceptions

index.php
---------
$frontController->setDefaultModule('default');
$frontController->throwExceptions(false);

Application/modules/default/controllers/FrontendController.php
--------------------------------------------------------------
<?php
class FrontendController extends Zend_Controller_Action {

	public function indexAction() {
		$this->view->message = "Hello world";
	}

	public function testErrorAction() {
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('missing param');
		}
	}
}

Application/modules/default/controllers/ErrorController.php
--------------------------------------------------------------
<?php
class ErrorController extends Zend_Controller_Action {
	
	public function errorAction()	{

		$errors = $this->_getParam('error_handler');

		//die($errors->type);				// display: EXCEPTION_OTHER
		//die(get_class($errors->exception));		// display: Zend_Controller_Action_Exception
		//die($errors->exception->getMessage());	// display: current exception message

		switch ($errors->type) {
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
				$this->getResponse()->setHttpResponseCode(404); // 404 error ( controller or action not found )
				break;
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
				$this->view->message = $errors->exception->getMessage();
				break;
			default:
				break;
		}

	}

	public function notAuthorizedAction() {
		$this->_helper->viewRenderer->setNoRender();
	}
}

TEST
http://zfsite/default/frontend/test-error/id/0
// display: missing param


http://codeutopia.net/blog/2009/03/02/handling-errors-in-zend-framework/
http://framework.zend.com/manual/en/zend.exception.html