LoginSignup
3
2

More than 5 years have passed since last update.

【CakePHP】try~catchでPDOExceptionが受け取れなかった話

Last updated at Posted at 2017-03-07

備忘録として。

ログインする際にもしもDBが止まっていたら、
という例外処理を作ろうとして、3時間くらい詰まりました。。。

認証はAuthコンポーネントを使って行いました。

修正前

LoginController.php
.
.
try{
    $user = $this->Auth->identify();

    if($user){
       $this->Auth->setUser($user);
       return $this->redirect(['controller' => 'hello');   
    }
} catch(PDOException $e) {
    $this->Flash->error('DBエラー');
}
.
.

例外処理に行かず500エラー...
Exceptionも試したけどダメだった...

なんでや!!!

修正後

LoginController.php
.
.
try{
    $user = $this->Auth->identify();

    if($user){
       $this->Auth->setUser($user);
       return $this->redirect(['controller' => 'hello');   
    }
} catch(\PDOException $e) {
    $this->Flash->error('DBエラー');
}
.
.

どこが変化したかわかりますかね?

} catch(\PDOException $e) {

この部分の PDOException の先頭にバックスラッシュをつけたら正常に動作するようになりました。
namespaceの問題(?)のようです。

参考・回答はこちら

CakePHP 3 - Catch Error
http://stackoverflow.com/questions/35311368/cakephp-3-catch-error

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2