捕获 Zend PDO 异常

发布于 2024-10-05 19:55:33 字数 653 浏览 4 评论 0原文

我想知道是否可以对 $application->bootstrap()->run();
进行 try catch 这将捕获 pdo 异常

这可行,但它捕获了每个异常,这是我不想要的。

try {
    $application->bootstrap()->run();
} catch (Exception $e) {
    print_r($e);
}

我遇到了一个令人讨厌的事件:抛出 pdo 异常并显示 application.ini 中的密码!

值得注意的是,我尝试过 PDOException,它没有被捕获。

我的数据库是在 bootstrap run() 中设置的,请

    try {
        $db = $this->getPluginResource('db')->getDbAdapter();
        Zend_Registry::set('dbAdapter', $db);
    } catch (Exception $e) {
        echo 1;exit;
    }

注意,如果我在本地输入错误的密码并运行应用程序,我不会看到 1,即使有错误报告,我也会看到一个空白页面。

I'm wondering if I can put a try catch about $application->bootstrap()->run();
that will catch pdo exceptions

This works, but it catches every exception, which I do not want.

try {
    $application->bootstrap()->run();
} catch (Exception $e) {
    print_r($e);
}

I had a nasty incident of pdo exception being throw and displaying the password from application.ini!

Worthy of note, I have tried PDOException, it doesnt get catched.

My DB is set up in bootstrap run() with

    try {
        $db = $this->getPluginResource('db')->getDbAdapter();
        Zend_Registry::set('dbAdapter', $db);
    } catch (Exception $e) {
        echo 1;exit;
    }

Note that if I put in the wrong password locally and run the app, I do not see 1, I see a blank page even with error reporting on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绅士风度i 2024-10-12 19:55:33

如果我正确理解了这个问题,这意味着您正在尝试捕获数据库连接异常

,这将像下面这些行一样简单:

try {
    $db = Zend_Db::factory('Pdo_Mysql', $parameters);
    $db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
    // perhaps a failed login credential, or perhaps the RDBMS is not running
} catch (Zend_Exception $e) {
    // perhaps factory() failed to load the specified Adapter class
}

基本上 getConnection 函数正在尝试使用参数连接到数据库,如果它失败了
它会抛出一个 Zend_Db_Adapter_Exception 并且如果成功连接它会返回 PDO 对象

类似地,您可以使用此模式来捕获 Zend_Db 异常或 PDO_Exceptions 在控制器类或模型中抛出此类错误,但不是整个应用程序

if i understand the question correctly , it mean you are trying to catch you db connection exception

and this would be as easy as these lines below :

try {
    $db = Zend_Db::factory('Pdo_Mysql', $parameters);
    $db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
    // perhaps a failed login credential, or perhaps the RDBMS is not running
} catch (Zend_Exception $e) {
    // perhaps factory() failed to load the specified Adapter class
}

basically getConnection function is trying to connect to db with the parameters , if it failed
it would throw an Zend_Db_Adapter_Exception and if succesfully connected it would return PDO object

similarly , you can use this pattern to catch you Zend_Db exceptions or PDO_Exceptions in controller classes or models that throw these kind of errors , but not the whole application

離殇 2024-10-12 19:55:33

转到有数据库代码的地方,并在该代码周围放置 try catch。如果您只想要 Pdo 异常,那么只捕获 PdoException。放置类似 catch(PdoExcetion_OR_What_Its_Name_Is $e) 的内容(并禁用屏幕上的错误输出。将错误写入日志文件)

Go where you have the database code and put try catch around that code. If you want only Pdo Exceptions then catch only PdoException. Put something like catch(PdoExcetion_OR_What_Its_Name_Is $e) (And disable the error output to the screen. Write your errors to a log file)

疧_╮線 2024-10-12 19:55:33

尝试捕获整个应用程序似乎很奇怪。禁用错误报告(例如 php 中的 display_errors .ini)会更好地停止泄露未捕获的异常中的任何敏感信息。

但回答你的问题:

try {
    $application->bootstrap()->run();
} catch (PDOException $e) {
    print_r($e);
}

这应该只捕获 PDO 异常。

It seems rather strange to try and catch a whole application. Disabling error reporting (such as display_errors in php.ini) would be a lot better to stop revealing any sensitive information from uncatched exceptions.

But to answer your question:

try {
    $application->bootstrap()->run();
} catch (PDOException $e) {
    print_r($e);
}

This should catch only PDO Exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文