diff --git a/core/database.php b/core/database.php index 5d53ae69..e09161c2 100644 --- a/core/database.php +++ b/core/database.php @@ -7,6 +7,8 @@ namespace Shimmie2; use FFSPHP\PDO; use FFSPHP\PDOStatement; +require_once __DIR__ . '/exceptions.php'; + enum DatabaseDriverID: string { case MYSQL = "mysql"; @@ -14,6 +16,20 @@ enum DatabaseDriverID: string case SQLITE = "sqlite"; } +class DatabaseException extends SCoreException +{ + public string $query; + public array $args; + + public function __construct(string $msg, string $query, array $args) + { + parent::__construct($msg); + $this->error = $msg; + $this->query = $query; + $this->args = $args; + } +} + /** * A class for controlled database access */ @@ -158,18 +174,13 @@ class Database public function _execute(string $query, array $args = []): PDOStatement { try { - $ret = $this->get_db()->execute( + return $this->get_db()->execute( "-- " . str_replace("%2F", "/", urlencode($_GET['q'] ?? '')). "\n" . $query, $args ); - if ($ret === false) { - throw new SCoreException("Query failed", $query); - } - /** @noinspection PhpIncompatibleReturnTypeInspection */ - return $ret; } catch (\PDOException $pdoe) { - throw new SCoreException($pdoe->getMessage(), $query); + throw new DatabaseException($pdoe->getMessage(), $query, $args); } } diff --git a/core/exceptions.php b/core/exceptions.php index 139b8670..9683d33c 100644 --- a/core/exceptions.php +++ b/core/exceptions.php @@ -9,15 +9,13 @@ namespace Shimmie2; */ class SCoreException extends \RuntimeException { - public ?string $query; public string $error; public int $http_code = 500; - public function __construct(string $msg, ?string $query = null) + public function __construct(string $msg) { parent::__construct($msg); $this->error = $msg; - $this->query = $query; } } diff --git a/core/util.php b/core/util.php index 62081a4d..432b8995 100644 --- a/core/util.php +++ b/core/util.php @@ -627,8 +627,6 @@ function _fatal_error(\Exception $e): void $version = VERSION; $message = $e->getMessage(); $phpver = phpversion(); - $query = is_subclass_of($e, "Shimmie2\SCoreException") ? $e->query : null; - $code = is_subclass_of($e, "Shimmie2\SCoreException") ? $e->http_code : 500; //$hash = exec("git rev-parse HEAD"); //$h_hash = $hash ? "
Hash: $hash" : ""; @@ -646,13 +644,21 @@ function _fatal_error(\Exception $e): void print("Message: $message\n"); - if ($query) { - print("Query: {$query}\n"); + if (is_a($e, DatabaseException::class)) { + print("Query: {$e->query}\n"); + print("Args: ".var_export($e->args, true)."\n"); } print("Version: $version (on $phpver)\n"); } else { - $q = $query ? "" : "
Query: " . html_escape($query); + $query = is_a($e, DatabaseException::class) ? $e->query : null; + $code = is_a($e, SCoreException::class) ? $e->http_code : 500; + + $q = ""; + if(is_a($e, DatabaseException::class)) { + $q .= "
Query: " . html_escape($query); + $q .= "
Args: " . html_escape(var_export($e->args, true)); + } if ($code >= 500) { error_log("Shimmie Error: $message (Query: $query)\n{$e->getTraceAsString()}"); } diff --git a/ext/random_image/main.php b/ext/random_image/main.php index 05c89294..75951215 100644 --- a/ext/random_image/main.php +++ b/ext/random_image/main.php @@ -25,10 +25,7 @@ class RandomImage extends Extension } $image = Image::by_random($search_terms); if (!$image) { - throw new SCoreException( - "Couldn't find any posts randomly", - Tag::implode($search_terms) - ); + throw new SCoreException("Couldn't find any posts randomly"); } if ($action === "download") { diff --git a/ext/tag_edit/main.php b/ext/tag_edit/main.php index 309c4ae7..986b5418 100644 --- a/ext/tag_edit/main.php +++ b/ext/tag_edit/main.php @@ -44,7 +44,7 @@ class TagSetException extends UserErrorException public function __construct(string $msg, ?string $redirect = null) { - parent::__construct($msg, null); + parent::__construct($msg); $this->redirect = $redirect; } }