From: Tim Düsterhus Date: Tue, 29 Sep 2015 19:34:36 +0000 (+0200) Subject: Update DatabaseException to make proper use of the new Exception handling X-Git-Tag: 3.0.0_Beta_1~2094^2~9 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=b8e0376cfd3e8e9a703f65deac84bc3678575c9d;p=GitHub%2FWoltLab%2FWCF.git Update DatabaseException to make proper use of the new Exception handling --- diff --git a/wcfsetup/install/files/lib/core.functions.php b/wcfsetup/install/files/lib/core.functions.php index 35b2121c3c..5d586439fb 100644 --- a/wcfsetup/install/files/lib/core.functions.php +++ b/wcfsetup/install/files/lib/core.functions.php @@ -42,6 +42,7 @@ namespace { } namespace wcf\functions\exception { + use wcf\system\exception\IExtraInformationException; use wcf\system\exception\SystemException; use wcf\system\WCF; use wcf\util\FileUtil; @@ -148,15 +149,15 @@ namespace wcf\functions\exception {
-

System Information:

+

System Information

-
PHP Version:
-
WCF Version:
-
Date:
-
Request URI:
-
Referrer:
-
User Agent:
-
Peak Memory Usage:
/ Byte (/ MiB)
+
PHP Version
+
WCF Version
+
Date
+
Request URI
+
Referrer
+
User Agent
+
Peak Memory Usage
/ Byte (/ MiB)
-

getPrevious() && !$first) { echo "Original "; } else if ($e->getPrevious() && $first) { echo "Final "; } ?>Error:

+

getPrevious() && !$first) { echo "Original "; } else if ($e->getPrevious() && $first) { echo "Final "; } ?>Error

getDescription()) { ?>

getDescription(); ?>

-
Error Class:
-
Error Message:
getMessage()); ?>
-
Error Code:
getCode()); ?>
-
File:
getFile())); ?> (getLine(); ?>)
+
Error Class
+
Error Message
getMessage()); ?>
+ getCode()) { ?>
Error Code
getCode()); ?>
+
File
getFile())); ?> (getLine(); ?>)
getExtraInformation() as list($key, $value)) { + echo "
".StringUtil::encodeHTML($key)."
".StringUtil::encodeHTML($value)."
"; + } + } ?> -
Stack Trace:
+
Stack Trace
5) return "[ ".StringUtil::HELLIP." ]"; + if (count($keys) > 5) return "[ ".count($keys)." items ]"; return '[ '.implode(', ', array_map(function ($item) { return $item.' => '; }, $keys)).']'; @@ -257,6 +263,8 @@ namespace wcf\functions\exception { if (!$ignorePaths) { $item['args'] = array_map(function ($item) { + if (!is_string($item)) return $item; + if (preg_match('~^'.preg_quote($_SERVER['DOCUMENT_ROOT'], '~').'~', $item)) { $item = sanitizePath($item); } diff --git a/wcfsetup/install/files/lib/system/database/Database.class.php b/wcfsetup/install/files/lib/system/database/Database.class.php index fba3294d3b..91eb805864 100644 --- a/wcfsetup/install/files/lib/system/database/Database.class.php +++ b/wcfsetup/install/files/lib/system/database/Database.class.php @@ -1,6 +1,9 @@ pdo->lastInsertId(); } catch (\PDOException $e) { - throw new DatabaseException("Cannot fetch last insert id: " . $e->getMessage(), $this); + throw new GenericDatabaseException("Cannot fetch last insert id", $e); } } @@ -150,7 +153,7 @@ abstract class Database { return $result; } catch (\PDOException $e) { - throw new DatabaseException("Cannot begin transaction: " . $e->getMessage(), $this); + throw new DatabaseTransactionException("Could not begin transaction", $e); } } @@ -179,7 +182,7 @@ abstract class Database { return $result; } catch (\PDOException $e) { - throw new DatabaseException("Cannot commit transaction: " . $e->getMessage(), $this); + throw new DatabaseTransactionException("Could not commit transaction", $e); } } @@ -207,7 +210,7 @@ abstract class Database { return $result; } catch (\PDOException $e) { - throw new DatabaseException("Cannot rollback transaction: " . $e->getMessage(), $this); + throw new DatabaseTransactionException("Could not roll back transaction", $e); } } @@ -228,7 +231,7 @@ abstract class Database { return new $this->preparedStatementClassName($this, $pdoStatement, $statement); } catch (\PDOException $e) { - throw new DatabaseException($e->getMessage(), $this, null, $statement); + throw new DatabaseQueryException("Could not prepare statement '".$statement."'", $e); } } diff --git a/wcfsetup/install/files/lib/system/database/DatabaseException.class.php b/wcfsetup/install/files/lib/system/database/DatabaseException.class.php index 377531e35a..d50a312dc8 100644 --- a/wcfsetup/install/files/lib/system/database/DatabaseException.class.php +++ b/wcfsetup/install/files/lib/system/database/DatabaseException.class.php @@ -13,6 +13,7 @@ use wcf\util\StringUtil; * @package com.woltlab.wcf * @subpackage system.database * @category Community Framework + * @deprecated 2.2 - Use \wcf\system\database\exception\DatabaseException */ class DatabaseException extends SystemException { /** @@ -128,28 +129,4 @@ class DatabaseException extends SystemException { public function getDBType() { return $this->DBType; } - - /** - * Prints the error page. - */ - public function show() { - $this->information .= 'sql type: ' . StringUtil::encodeHTML($this->getDBType()) . '
'; - $this->information .= 'sql error: ' . StringUtil::encodeHTML($this->getErrorDesc()) . '
'; - $this->information .= 'sql error number: ' . StringUtil::encodeHTML($this->getErrorNumber()) . '
'; - $this->information .= 'sql version: ' . StringUtil::encodeHTML($this->getSQLVersion()) . '
'; - if ($this->preparedStatement !== null) { - $this->information .= 'sql query: ' . StringUtil::encodeHTML($this->preparedStatement->getSQLQuery()) . '
'; - $parameters = $this->preparedStatement->getSQLParameters(); - if (!empty($parameters)) { - foreach ($parameters as $index => $parameter) { - $this->information .= 'sql query parameter ' . $index . ':' . StringUtil::encodeHTML($parameter) . '
'; - } - } - } - else if ($this->sqlQuery !== null) { - $this->information .= 'sql query: ' . StringUtil::encodeHTML($this->sqlQuery) . '
'; - } - - parent::show(); - } } diff --git a/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php b/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php index 3fce425a53..ea28c2b85a 100644 --- a/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php +++ b/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php @@ -1,5 +1,6 @@ setAttributes(); } catch (\PDOException $e) { - throw new DatabaseException("Connecting to MySQL server '".$this->host."' failed:\n".$e->getMessage(), $this); + throw new GenericDatabaseException("Connecting to MySQL server '".$this->host."' failed", $e); } } diff --git a/wcfsetup/install/files/lib/system/database/PostgreSQLDatabase.class.php b/wcfsetup/install/files/lib/system/database/PostgreSQLDatabase.class.php index 035cab49a3..6351cce293 100644 --- a/wcfsetup/install/files/lib/system/database/PostgreSQLDatabase.class.php +++ b/wcfsetup/install/files/lib/system/database/PostgreSQLDatabase.class.php @@ -1,5 +1,6 @@ setAttributes(); } catch (\PDOException $e) { - throw new DatabaseException("Connecting to PostgreSQL server '".$this->host."' failed:\n".$e->getMessage(), $this); + throw new GenericDatabaseException("Connecting to PostgreSQL server '".$this->host."' failed", $e); } // set connection character set @@ -72,7 +73,7 @@ class PostgreSQLDatabase extends Database { return $this->pdo->lastInsertId('"' . $table . '_' . $field . '_seq"'); } catch (\PDOException $e) { - throw new DatabaseException("Can not fetch last insert id", $this); + throw new GenericDatabaseException("Can not fetch last insert id", $this); } } diff --git a/wcfsetup/install/files/lib/system/database/exception/DatabaseException.class.php b/wcfsetup/install/files/lib/system/database/exception/DatabaseException.class.php new file mode 100644 index 0000000000..565b3fc7f5 --- /dev/null +++ b/wcfsetup/install/files/lib/system/database/exception/DatabaseException.class.php @@ -0,0 +1,21 @@ + + * @package com.woltlab.wcf + * @subpackage system.database.exception + * @category Community Framework + */ +class DatabaseException extends \wcf\system\database\DatabaseException { + /** + * @see \Exception::__construct() + */ + public function __construct($message, \PDOException $previous = null) { + \Exception::__construct($message, 0, $previous); + } +} diff --git a/wcfsetup/install/files/lib/system/database/exception/DatabaseQueryException.class.php b/wcfsetup/install/files/lib/system/database/exception/DatabaseQueryException.class.php new file mode 100644 index 0000000000..9e6c568acc --- /dev/null +++ b/wcfsetup/install/files/lib/system/database/exception/DatabaseQueryException.class.php @@ -0,0 +1,16 @@ + + * @package com.woltlab.wcf + * @subpackage system.database.exception + * @category Community Framework + */ +class DatabaseQueryException extends DatabaseException { + +} diff --git a/wcfsetup/install/files/lib/system/database/exception/DatabaseQueryExecutionException.class.php b/wcfsetup/install/files/lib/system/database/exception/DatabaseQueryExecutionException.class.php new file mode 100644 index 0000000000..091222ce2b --- /dev/null +++ b/wcfsetup/install/files/lib/system/database/exception/DatabaseQueryExecutionException.class.php @@ -0,0 +1,49 @@ + + * @package com.woltlab.wcf + * @subpackage system.database.exception + * @category Community Framework + */ +class DatabaseQueryExecutionException extends DatabaseQueryException implements IExtraInformationException { + /** + * Parameters that were passed to execute(). + * @var array + */ + protected $parameters = []; + + /** + * @see \Exception::__construct() + */ + public function __construct($message, $parameters, \PDOException $previous = null) { + \Exception::__construct($message, 0, $previous); + + $this->parameters = $parameters; + } + + /** + * Returns the parameters that were passed to execute(). + * + * @return array + */ + public function getParameters() { + return $this->parameters; + } + + /** + * @see \wcf\system\exception\IExtraInformationException::getExtraInformation() + */ + public function getExtraInformation() { + return array_map(function ($val) { + static $i = 0; + return [ 'Query Parameter '.(++$i), $val ]; + }, $this->getParameters()); + } +} diff --git a/wcfsetup/install/files/lib/system/database/exception/DatabaseTransactionException.class.php b/wcfsetup/install/files/lib/system/database/exception/DatabaseTransactionException.class.php new file mode 100644 index 0000000000..4d5ea38075 --- /dev/null +++ b/wcfsetup/install/files/lib/system/database/exception/DatabaseTransactionException.class.php @@ -0,0 +1,16 @@ + + * @package com.woltlab.wcf + * @subpackage system.database.exception + * @category Community Framework + */ +class DatabaseTransactionException extends DatabaseException { + +} diff --git a/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php b/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php index 6fe37efd64..1f0ff086a3 100644 --- a/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php +++ b/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php @@ -2,7 +2,8 @@ namespace wcf\system\database\statement; use wcf\system\benchmark\Benchmark; use wcf\system\database\Database; -use wcf\system\database\DatabaseException; +use wcf\system\database\exception\DatabaseQueryException; +use wcf\system\database\exception\DatabaseQueryExecutionException; use wcf\system\exception\SystemException; use wcf\system\WCF; @@ -70,7 +71,7 @@ class PreparedStatement { return call_user_func_array(array($this->pdoStatement, $name), $arguments); } catch (\PDOException $e) { - throw new DatabaseException('Could not handle prepared statement: '.$e->getMessage(), $this->database, $this); + throw new DatabaseQueryException("Could call '".$name."' on '".$this->query."'", $e); } } @@ -86,12 +87,11 @@ class PreparedStatement { try { if (WCF::benchmarkIsEnabled()) Benchmark::getInstance()->start($this->query, Benchmark::TYPE_SQL_QUERY); - if (empty($parameters)) $result = $this->pdoStatement->execute(); - else $result = $this->pdoStatement->execute($parameters); + $result = $this->pdoStatement->execute($parameters); if (!$result) { $errorInfo = $this->pdoStatement->errorInfo(); - throw new DatabaseException('Could not execute prepared statement: '.$errorInfo[0].' '.$errorInfo[2], $this->database, $this); + throw new DatabaseQueryExecutionException("Could not execute statement '".$this->query."': ".$errorInfo[0].' '.$errorInfo[2], $parameters); } if (WCF::benchmarkIsEnabled()) Benchmark::getInstance()->stop(); @@ -99,7 +99,7 @@ class PreparedStatement { catch (\PDOException $e) { if (WCF::benchmarkIsEnabled()) Benchmark::getInstance()->stop(); - throw new DatabaseException('Could not execute prepared statement: '.$e->getMessage(), $this->database, $this); + throw new DatabaseQueryExecutionException("Could not execute statement '".$this->query."'", $parameters, $e); } } @@ -192,7 +192,7 @@ class PreparedStatement { return $this->pdoStatement->rowCount(); } catch (\PDOException $e) { - throw new DatabaseException("Can not fetch affected rows: ".$e->getMessage(), $this); + throw new DatabaseQueryException("Could fetch affected rows for '".$this->query."'", $e); } } diff --git a/wcfsetup/install/files/lib/system/exception/IExtraInformationException.class.php b/wcfsetup/install/files/lib/system/exception/IExtraInformationException.class.php new file mode 100644 index 0000000000..882cd41139 --- /dev/null +++ b/wcfsetup/install/files/lib/system/exception/IExtraInformationException.class.php @@ -0,0 +1,23 @@ + + * @package com.woltlab.wcf + * @subpackage system.exception + * @category Community Framework + */ +interface IExtraInformationException { + /** + * Returns an array of (key, value) tuples with extra information to show + * in the human readable error log. + * Avoid including sensitive information (such as private keys or passwords). + * + * @return array + */ + public function getExtraInformation(); +}