有些例外没有被发现

时间:2014-12-17 17:42:46

标签: php exception try-catch

我正在创建一个简单的API。我有一个index.php文件,所有对API的请求都会通过。 index.php处理请求的参数以确定要使用的控制器和操作。所有这些都包含在try/catch块中,因此每当我需要为客户端生成错误消息时,我都可以throw new Exception()。这已按预期工作,但现在我已经创建了一个用户类来处理用户注册和登录。在注册操作中,我检查从客户端应用程序传递的用户名是否已经存在于数据库中,如果存在,我throw new Exception('Username already exists!')。但是,catch没有抓到这个。相反,我得到了正常的Fatal error: uncaught exception消息。这是我的index.php

代码
include_once 'models/UserItem.php';

try {

    $enc_request = $_REQUEST['enc_request'];

    $app_id = $_REQUEST['app_id'];
    if( !isset($applications[$app_id]) ) {
      throw new Exception('Application does not exist!');
    }


    $params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $applications[$app_id], base64_decode($enc_request), MCRYPT_MODE_ECB )));
    if( $params == false || isset($params->controller) == false || isset($params->action) == false ) {
        throw new Exception('Request is not valid');
    }

    $params = (array) $params;

    $controller = ucfirst(strtolower($params['controller'])); //User


    $action = strtolower($params['action']).'Action'; //registerAction

    if( file_exists("controllers/{$controller}.php") ) {
        include_once "controllers/{$controller}.php";
    } else {
        throw new Exception('Controller is invalid.');
    }


    $controller = new $controller($params);

    if( method_exists($controller, $action) === false ) {
        throw new Exception('Action is invalid.');
    }

    $result['data'] = $controller->$action();
    $result['success'] = true;

} catch( Exception $e ) {
    $result = array();
    $result['success'] = false;
    $result['errormsg'] = $e->getMessage();
}

发送到请求的参数位于以下数组中:

array(
    'controller' => 'user',
    'action' => 'register',
    'username' => $_POST['username'],
    'userpass' => $_POST['userpass']
);

index.php中的代码然后调用我的registerAction()中的user controller

public function registerAction() {
    $user = new UserItem();
    $user->username = $this->_params['username'];
    $user->password = $this->_params['userpass'];

    $user->user_id = $user->save();

    return $user->toArray();
}

然后,此代码会调用save中的方法UserItem model,即:

public function save() {
        $db = new Database();
        $db->query('select * from users where username = :user');
        $db->bind(':user', $this->username);
        $r = $db->single();

        if ( $r ) {
            throw new Exception('Username already exists! Please choose another.');
        }

        else {
            $db->query('insert into users(username, password) values(:username, :password)');
            $db->bind(':username', $this->username);
            $db->bind(':password', $this->password);    

            $r = $db->execute();

            $user_array = $this->toArray();

            if ( !$r ) {
                throw new Exception('There was a problem creating your account. Please try again later.');
            }
        }

        return $db->lastInsertId();

}

服务器正在抛出异常,它没有被正确捕获。有没有人在try / catch中看到我在这里做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

在这里你抛出一个异常,但你没有发现异常。要实现这一点,您必须在try / catch块内部实现以处理捕获的异常。这是一个实现尝试的例子。

try{
$user->user_id = $user->save();
}catch(Exception $e){
echo "caught Exception :".$->getMessage();
}

我希望它能解决你的问题。谢谢。