2017-01-09 22 views
0

をキャッチされていませんTokenExceptionキャッチをスキップし、例外のキャッチを入力して、トークンエラーメッセージを表示します。カスタム例外は、このカスタム例外を持つ

ここで何が間違っていますか?

EDIT

ここでは、コードをより詳細にあります。これは、関連するパブリックAPIの親コントローラである

:それはCodeIgniterのMVCパターンを使用しています

class Apicontroller extends MX_Controller 
{ 

    /** 
    * 
    * @var stdClass $user 
    */ 
    protected $user; 


    /** 
    * Load user model 
    */ 
    public function __construct() 
    { 
     $this->load->model('users_model', 'users'); 
    } 


    protected function output($data) 
    { 
     header('Content-Type: application/json'); 
     header('Access-Control-Allow-Origin: *'); 
     echo json_encode($data); 
    } 

    // other code... 

} 

例外がキャッチされていないコード:の妥当性をチェックするために使用

require_once __DIR__ . '/Apicontroller.php'; 
require_once __DIR__ . '/../TokenException.php'; 


class Apisellercontroller extends Apicontroller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('apiseller_model', 'apiseller'); 
     $this->load->model('log_model', 'logger'); 
     $this->load->model('alerts_model', 'alerts'); 
    } 

    // inserts/updates data of a visit made by a sales agent to a client from an external app 
    public function visit() 
    { 
     try { 

      $res = []; 

      // get api token and user 
      $user = $this->input->post('user'); 
      $token = $this->input->post('token'); 
      $visit = $this->input->post('data'); 

      $this->validateRequest($user, $token, $visit); 

      // Get user by token from the users model 
      $this->user = $this->users->getByToken($token); 

      $this->apiseller->setUser($this->user); 

      $visit['sellers_id'] = $this->apiseller->getSeller()->id; 

      // Insert/update 
      $this->load->model('visits_model'); 

      $res['id'] = $this->visits_model->save($visit); 

     } 
     catch (TokenException $e) { 

      $res['relogin'] = 1; 
      $res['error'] = $e->getMessage() . ' token: ' . $token . ' user: ' . $user; 

     } 
     catch (Exception $e) { 

      $res['error'] = $e->getMessage() . ' token: ' . $token . ' user: ' . $user; 

     } 
     finally { 
      $this->output($res); 
     } 
    } 

    private function validateRequest($user, $token, $data) 
    { 
     if (empty($user)) { 
      throw new Exception('User id not received'); 
     } 
     if (empty($token)) { 
      throw new Exception('Session token not received'); 
     } 
     if (empty($data)) { 
      throw new Exception('Request data not available'); 
     } 

     // Check token validity for user 
     $this->apiseller->checkTokenValid($user, $token); 
    } 

    // other methods 
} 

モデルトークン(トークンが無効な場合に備えてTokenExcepcionをスローします):

require_once __DIR__ . '/Api_model.php'; 
require_once __DIR__ . '/../modules/api/TokenException.php'; 

class Apiseller_model extends Api_model 
{ 

    public function checkTokenValid($currentUser, $token) 
    { 
     $user = $this->getUserByToken($token); 
     if (empty($user)) { 
      throw new TokenException('User not found for specified token', $token); 
     } 

     if ($user->name != $currentUser) { 
      throw new TokenException("Invalid token. It doesn't match the specified user", $token); 
     } 
    } 

    // other code... 

} 
+0

それはあなたがそれだと思うものにならないことがあります。それは、それはあなたのコードの他の部分に包まれた意味します。 'get_class($ e)'を記録して、それがTokenExceptionであることを確認してください。 – aynber

+0

@aynber: "Exception"を表示します。 TokenExceptionとしてスローされていないように見えます。 – user3514092

答えて

1

試しましたか? parent :: __ construct($ message、1);最初の行として?そうでない場合、私はそれが以前に設定した属性よりも優先されます理解...

class TokenException extends Exception 
{ 
    public $token; 

    public function __construct($message, $token) 
    { 
     parent::__construct($message, 1); 
     $this->message = $message; 
     $this->token = $token; 
    } 
} 
+0

あなたが言ったことを試しました。あなたは、私が親の構築物を最初に呼び出すべきであるという点では正しいです。とにかく、TokenExceptionではなくExceptionとして例外をスローしています – user3514092

+0

名前空間とは何ですか?トークン例外がネームスペース内で宣言されている場合は、「使用」を使用してそれを含む必要があります。 – splig

+0

このケースでは名前空間を使用していません – user3514092

1

例外がスローされたbeeingて明らかにTokenExceptionのインスタンスではないので、私はここあなたが、見にスタックトレースを見てみなければならないことを示唆しています例外は本当に来てから:

http://php.net/manual/en/exception.gettraceasstring.php

echo $e->getTraceAsString();die; 
あなたの例外は「前」として渡された場合にも確認することができ

http://php.net/manual/en/exception.getprevious.php

var_dump($e->getPrevious());die;