アットウィキロゴ

exception

<?php
class RxtException extends Exception {

protected $errCode;      // 自定义error code
protected $errMessage;   // 自定义error message
//protected $errTpye;      // 自定义error type

public function __construct($code = '0000') {
	$this->errCode = $code;
	parent::__construct ();
}
public function getErrorMessage() {
	
	// 自定义code四位字符,
	// 第1位:1=>fatal,2=>error,3=>warning,4=>attention
	// 第2位:1=>controller模块,2=>model模块
	// 第3,4位:按序号排列
	switch ($this->errCode) {
		case '1001' :
			$this->errMessage = 'testerror1';
			break;
		case '1002' :
			$this->errMessage = 'testerror2';
			break;
		case '0000' :
			$this->errMessage = 'unexcepted error';
			break;
		default :
			$this->errMessage = 'default error';
			break;
	}
	return $this->errMessage;
}

public function getErrorCode() {
	return $this->errCode;
}
}
?>


<?php

require_once APPLICATION_PATH.'/helpers/RxtException.php';

class TestExceptionController extends Zend_Controller_Action{

public function indexAction(){
	try {
		//throw new RxtException('1001');
		throw new Exception();
	} catch (RxtException $e) { //首先捕获rxtRxtException
		$this->forward('exception','global', '',array('exception'=>$e));
	} catch (Exception $e) {    //如果捕获不到,作为rxtRxtException中的默认错误$code = '0000'
		$e = new RxtException();
		$this->forward('exception','global', '',array('exception'=>$e));
	}
	


}
}

?>


<?php
class GlobalController extends Zend_Controller_Action {
public function init() {
	/* Initialize action controller here */
}
public function exceptionAction() {

	$param = $this->getRequest ()->getParam ( 'exception' );
	//echo "<pre>";
	$stringValue = array(
			'errcode'=>json_encode($param->getErrorCode()),
			'errmessage'=>json_encode($param->getErrorMessage()),
			);
	
	echo json_encode ( $stringValue );
	exit ();
}
}

タグ:

+ タグ編集
  • タグ:
最終更新:2013年07月25日 17:19