Changeset 698 for trunk/lib


Ignore:
Timestamp:
Aug 8, 2019 10:36:39 PM (5 years ago)
Author:
anonymous
Message:

Improve error handling and display

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/PDO.inc.php

    r693 r698  
    192192            $this->dbh = new \PDO($dsn, $this->getParam('db_user'), $this->getParam('db_pass'), $options);
    193193        } catch (\PDOException $e) {
    194             $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage());
    195             // sprintf('PDO error: Could not connect to server (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)', $this->getParam('db_server'), $this->getParam('db_name'), $this->getParam('db_user'), ('' == $this->getParam('db_pass') ? 'NO' : 'YES'));
     194            $mysql_error_msg = sprintf('PDO connect %s: %s (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)',
     195                get_class($e),
     196                $e->getMessage(),
     197                $this->getParam('db_server'),
     198                $this->getParam('db_name'),
     199                $this->getParam('db_user'),
     200                ('' == $this->getParam('db_pass') ? 'NO' : 'YES')
     201            );
    196202            $app->logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
    197203
    198204            // Print helpful or pretty error?
    199             if ($this->getParam('db_debug')) {
    200                 echo $mysql_error_msg . "\n";
     205            if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
     206                if (!$app->isCLI()) {
     207                    printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s</pre>', $mysql_error_msg);
     208                }
    201209            }
    202210
    203211            // Die if db_die_on_failure = true, or just continue without connection.
    204212            return $this->_fail();
    205 
    206             // throw new \PDOException($e->getMessage(), (int)$e->getCode());
    207213        }
    208214
     
    233239    public function close()
    234240    {
     241        $app =& App::getInstance();
     242
    235243        if (!$this->_connected) {
     244            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    236245            return false;
    237246        }
     
    268277    public function ping()
    269278    {
     279        $app =& App::getInstance();
     280
    270281        if (!$this->_connected) {
    271            return false;
     282            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
     283            return false;
    272284        }
    273285
     
    362374
    363375        if (!$this->_connected) {
    364            return false;
     376            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
     377            return false;
    365378        }
    366379
     
    372385                dump($debugqry, true, SC_DUMP_PRINT_R, __FILE__, __LINE__);
    373386            } else {
    374                 echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n";
     387                echo "<!-- ----------------- PDO query $this->_query_count ---------------------\n$debugqry\n-->\n";
    375388            }
    376389        }
     
    387400        try {
    388401            $stmt = $this->dbh->query($query);
    389         } catch (\PDOException $e) {
    390             $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage());
    391             $app->logMsg(sprintf('PDOException (%s): %s in query: %s', $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
    392             if ($this->getParam('db_debug')) {
     402            if (!$stmt) {
     403                throw new Exception('PDO::query returned false');
     404            }
     405        } catch (\Exception $e) {
     406            $app->logMsg(sprintf('PDO query %s (%s): %s in query: %s', get_class($e), $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
     407            if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
    393408                if (!$app->isCLI()) {
    394                     printf('<pre style="padding:2em; background:#ddd; font:9px monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
     409                    printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
    395410                }
    396411            }
     
    416431
    417432        if (!$this->_connected) {
    418            return false;
     433            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
     434            return false;
    419435        }
    420436
     
    423439        $debugqry = preg_replace("/\n[\t ]+/u", "\n", $query);
    424440        if ($this->getParam('db_always_debug')) {
    425             echo "<!-- ----------------- Prepare $this->_query_count ---------------------\n$debugqry\n-->\n";
     441            echo "<!-- ----------------- PDO prepare $this->_query_count ---------------------\n$debugqry\n-->\n";
    426442        }
    427443
     
    437453        try {
    438454            $stmt = $this->dbh->prepare($query, ...$params);
     455            if (!$stmt) {
     456                throw new Exception('PDO::query returned false');
     457            }
    439458        } catch (\PDOException $e) {
    440             $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage());
    441             $app->logMsg(sprintf('PDOException (%s): %s in prepare: %s', $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
    442             if ($this->getParam('db_debug')) {
     459            $app->logMsg(sprintf('PDO prepare %s (%s): %s in query: %s', get_class($e), $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__);
     460            if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
    443461                if (!$app->isCLI()) {
    444                     printf('<pre style="padding:2em; background:#ddd; font:9px monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
     462                    printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));
    445463                }
    446464            }
     
    509527
    510528        if (!$this->_connected) {
     529            $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    511530            return false;
    512531        }
Note: See TracChangeset for help on using the changeset viewer.