をキャッチされていません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...
}
それはあなたがそれだと思うものにならないことがあります。それは、それはあなたのコードの他の部分に包まれた意味します。 'get_class($ e)'を記録して、それがTokenExceptionであることを確認してください。 – aynber
@aynber: "Exception"を表示します。 TokenExceptionとしてスローされていないように見えます。 – user3514092