【问题标题】:catch PDOException in laravel在 laravel 中捕获 PDOException
【发布时间】:2014-02-05 21:19:01
【问题描述】:

我目前正在尝试在 Laravel 中捕获 PDOException,并允许我的应用程序在捕获错误后继续运行。我在不同的位置尝试了多次尝试捕获,但在导致应用失败之前我无法捕获错误。

这是我的模型:

class Oracle extends Eloquent {

  protected $connection = 'oracle';
  protected $user = '';
  protected $table = '';
  public $timestamps = false;

  public function __construct(array $attributes = array()) {
    parent::__construct($attributes);
    DB::reconnect('oracle');
    $this->table = Config::get('database.connections.oracle.table');
    $this->user = Config::get('database.connections.oracle.username');
  }

  public function scopegetPerms($callback, $db) {
    return $callback->select('*')->where('grantee', '=', strtoupper(trim($this->user)))->orderBy('granted_role');
  }

}

我正在即时更新oracle 连接设置,并使用DB::reconnect 函数获取更改。一切都按预期工作。我希望能够捕获无效的用户/密码异常,并在以后继续通知用户。但我试图捕捉异常并没有奏效。

我尝试在 DB::reconnect() 和查询本身周围放置一个 try/catch。

这是我得到的错误:

{"error":{"type":"PDOException","message":"ORA-01017: invalid username\/password; logon denied"

【问题讨论】:

    标签: php pdo laravel


    【解决方案1】:

    当你运行时

    new Oracle

    像这样在 try{}catch{} 中包围它:

    try {
    new Oracle; 
    }catch(PDOException $e){ 
    //Do whatever. 
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 2012-11-16
      • 2014-10-11
      相关资源
      最近更新 更多