簡體   English   中英

某些異常未被捕獲

[英]Some exceptions are not being caught

我正在創建一個簡單的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的代碼在我的user controller調用registerAction()

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塊內部實現以處理捕獲的異常。 這是一個實施嘗試的示例。

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

我希望它能解決您的問題。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM