From fb5d0db0376f8f75fe6745fd1c94d5b325be4f41 Mon Sep 17 00:00:00 2001 From: Frank Karlitschek Date: Fri, 23 Nov 2012 16:40:28 +0100 Subject: [PATCH 1/3] =?UTF-8?q?prettier=20error=20output.=20Let=C2=B4s=20s?= =?UTF-8?q?ee=20how=20many=20more=20low=20hanging=20fruits=20I=20find=20on?= =?UTF-8?q?=20my=20way=20to=20the=20real=20bug=20that=20I=20want=20to=20fi?= =?UTF-8?q?x=20;-)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db.php | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/db.php b/lib/db.php index de42626563..687c49a021 100644 --- a/lib/db.php +++ b/lib/db.php @@ -168,7 +168,10 @@ class OC_DB { try{ self::$PDO=new PDO($dsn, $user, $pass, $opts); }catch(PDOException $e) { - echo( 'can not connect to database, using '.$type.'. ('.$e->getMessage().')'); + $error['error']='can not connect to database, using '.$type.'. ('.$e->getMessage().')'; + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); die(); } // We always, really always want associative arrays @@ -263,9 +266,12 @@ class OC_DB { // Die if we could not connect if( PEAR::isError( self::$MDB2 )) { - echo( 'can not connect to database, using '.$type.'. ('.self::$MDB2->getUserInfo().')'); OC_Log::write('core', self::$MDB2->getUserInfo(), OC_Log::FATAL); OC_Log::write('core', self::$MDB2->getMessage(), OC_Log::FATAL); + $error['error']='can not connect to database, using '.$type.'. ('.self::$MDB2->getUserInfo().')'; + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); die(); } @@ -326,7 +332,11 @@ class OC_DB { $entry .= 'Offending command was: '.htmlentities($query).'
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: '.$entry); - die( $entry ); + $error['error']=$entry; + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); + die(); } }else{ try{ @@ -336,7 +346,11 @@ class OC_DB { $entry .= 'Offending command was: '.htmlentities($query).'
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: '.$entry); - die( $entry ); + $error['error']=$entry; + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); + die(); } $result=new PDOStatementWrapper($result); } @@ -449,7 +463,11 @@ class OC_DB { // Die in case something went wrong if( $definition instanceof MDB2_Schema_Error ) { - die( $definition->getMessage().': '.$definition->getUserInfo()); + $error['error']=$definition->getMessage().': '.$definition->getUserInfo(); + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); + die(); } if(OC_Config::getValue('dbtype', 'sqlite')==='oci') { unset($definition['charset']); //or MDB2 tries SHUTDOWN IMMEDIATE @@ -461,8 +479,11 @@ class OC_DB { // Die in case something went wrong if( $ret instanceof MDB2_Error ) { - echo (self::$MDB2->getDebugOutput()); - die ($ret->getMessage() . ': ' . $ret->getUserInfo()); + $error['error']=self::$MDB2->getDebugOutput().' '.$ret->getMessage() . ': ' . $ret->getUserInfo(); + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); + die(); } return true; @@ -575,7 +596,11 @@ class OC_DB { $entry .= 'Offending command was: ' . $query . '
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: '.$entry); - die( $entry ); + $error['error']=$entry; + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); + die(); } if($result->numRows() == 0) { @@ -607,7 +632,11 @@ class OC_DB { $entry .= 'Offending command was: ' . $query.'
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: ' . $entry); - die( $entry ); + $error['error']=$entry; + $error['hint']=''; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); + die(); } return $result->execute(); From 0f6181627835572ce70fddfc4decc14b1213a715 Mon Sep 17 00:00:00 2001 From: Frank Karlitschek Date: Sat, 24 Nov 2012 18:07:26 +0100 Subject: [PATCH 2/3] A new function to create nice error page. And use it for fatal db errors --- lib/db.php | 48 ++++++++---------------------------------------- lib/template.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/lib/db.php b/lib/db.php index 687c49a021..09b4934940 100644 --- a/lib/db.php +++ b/lib/db.php @@ -168,11 +168,7 @@ class OC_DB { try{ self::$PDO=new PDO($dsn, $user, $pass, $opts); }catch(PDOException $e) { - $error['error']='can not connect to database, using '.$type.'. ('.$e->getMessage().')'; - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( 'can not connect to database, using '.$type.'. ('.$e->getMessage().')' ); } // We always, really always want associative arrays self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); @@ -268,11 +264,7 @@ class OC_DB { if( PEAR::isError( self::$MDB2 )) { OC_Log::write('core', self::$MDB2->getUserInfo(), OC_Log::FATAL); OC_Log::write('core', self::$MDB2->getMessage(), OC_Log::FATAL); - $error['error']='can not connect to database, using '.$type.'. ('.self::$MDB2->getUserInfo().')'; - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( 'can not connect to database, using '.$type.'. ('.self::$MDB2->getUserInfo().')' ); } // We always, really always want associative arrays @@ -332,11 +324,7 @@ class OC_DB { $entry .= 'Offending command was: '.htmlentities($query).'
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: '.$entry); - $error['error']=$entry; - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( $entry ); } }else{ try{ @@ -346,11 +334,7 @@ class OC_DB { $entry .= 'Offending command was: '.htmlentities($query).'
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: '.$entry); - $error['error']=$entry; - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( $entry ); } $result=new PDOStatementWrapper($result); } @@ -463,11 +447,7 @@ class OC_DB { // Die in case something went wrong if( $definition instanceof MDB2_Schema_Error ) { - $error['error']=$definition->getMessage().': '.$definition->getUserInfo(); - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( $definition->getMessage().': '.$definition->getUserInfo() ); } if(OC_Config::getValue('dbtype', 'sqlite')==='oci') { unset($definition['charset']); //or MDB2 tries SHUTDOWN IMMEDIATE @@ -479,11 +459,7 @@ class OC_DB { // Die in case something went wrong if( $ret instanceof MDB2_Error ) { - $error['error']=self::$MDB2->getDebugOutput().' '.$ret->getMessage() . ': ' . $ret->getUserInfo(); - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( self::$MDB2->getDebugOutput().' '.$ret->getMessage() . ': ' . $ret->getUserInfo() ); } return true; @@ -596,11 +572,7 @@ class OC_DB { $entry .= 'Offending command was: ' . $query . '
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: '.$entry); - $error['error']=$entry; - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( $entry ); } if($result->numRows() == 0) { @@ -632,11 +604,7 @@ class OC_DB { $entry .= 'Offending command was: ' . $query.'
'; OC_Log::write('core', $entry, OC_Log::FATAL); error_log('DB error: ' . $entry); - $error['error']=$entry; - $error['hint']=''; - $errors[]=$error; - OC_Template::printGuestPage("", "error", array("errors" => $errors)); - die(); + OC_Template::printErrorPage( $entry ); } return $result->execute(); diff --git a/lib/template.php b/lib/template.php index a10cabf593..868d5f2ba2 100644 --- a/lib/template.php +++ b/lib/template.php @@ -496,4 +496,19 @@ class OC_Template{ } return $content->printPage(); } + + /** + * @brief Print a fatal error page and terminates the script + * @param string $error The error message to show + * @param string $hint An option hint message + */ + public static function printErrorPage( $error, $hint = '' ) { + $error['error']=$error; + $error['hint']=$hint; + $errors[]=$error; + OC_Template::printGuestPage("", "error", array("errors" => $errors)); + die(); + } + + } From e450933650762145314ab01f1ecd0a1a373e9ccd Mon Sep 17 00:00:00 2001 From: Frank Karlitschek Date: Sat, 24 Nov 2012 18:25:05 +0100 Subject: [PATCH 3/3] remove left over tag --- lib/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/db.php b/lib/db.php index 09b4934940..f79768a664 100644 --- a/lib/db.php +++ b/lib/db.php @@ -168,7 +168,7 @@ class OC_DB { try{ self::$PDO=new PDO($dsn, $user, $pass, $opts); }catch(PDOException $e) { - OC_Template::printErrorPage( 'can not connect to database, using '.$type.'. ('.$e->getMessage().')' ); + OC_Template::printErrorPage( 'can not connect to database, using '.$type.'. ('.$e->getMessage().')' ); } // We always, really always want associative arrays self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);