[core] fix error in error handling

This commit is contained in:
Shish 2024-01-04 22:48:56 +00:00
parent b060accc44
commit 48b3de3c6e
5 changed files with 32 additions and 20 deletions

View file

@ -7,6 +7,8 @@ namespace Shimmie2;
use FFSPHP\PDO; use FFSPHP\PDO;
use FFSPHP\PDOStatement; use FFSPHP\PDOStatement;
require_once __DIR__ . '/exceptions.php';
enum DatabaseDriverID: string enum DatabaseDriverID: string
{ {
case MYSQL = "mysql"; case MYSQL = "mysql";
@ -14,6 +16,20 @@ enum DatabaseDriverID: string
case SQLITE = "sqlite"; 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 * A class for controlled database access
*/ */
@ -158,18 +174,13 @@ class Database
public function _execute(string $query, array $args = []): PDOStatement public function _execute(string $query, array $args = []): PDOStatement
{ {
try { try {
$ret = $this->get_db()->execute( return $this->get_db()->execute(
"-- " . str_replace("%2F", "/", urlencode($_GET['q'] ?? '')). "\n" . "-- " . str_replace("%2F", "/", urlencode($_GET['q'] ?? '')). "\n" .
$query, $query,
$args $args
); );
if ($ret === false) {
throw new SCoreException("Query failed", $query);
}
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $ret;
} catch (\PDOException $pdoe) { } catch (\PDOException $pdoe) {
throw new SCoreException($pdoe->getMessage(), $query); throw new DatabaseException($pdoe->getMessage(), $query, $args);
} }
} }

View file

@ -9,15 +9,13 @@ namespace Shimmie2;
*/ */
class SCoreException extends \RuntimeException class SCoreException extends \RuntimeException
{ {
public ?string $query;
public string $error; public string $error;
public int $http_code = 500; public int $http_code = 500;
public function __construct(string $msg, ?string $query = null) public function __construct(string $msg)
{ {
parent::__construct($msg); parent::__construct($msg);
$this->error = $msg; $this->error = $msg;
$this->query = $query;
} }
} }

View file

@ -627,8 +627,6 @@ function _fatal_error(\Exception $e): void
$version = VERSION; $version = VERSION;
$message = $e->getMessage(); $message = $e->getMessage();
$phpver = phpversion(); $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"); //$hash = exec("git rev-parse HEAD");
//$h_hash = $hash ? "<p><b>Hash:</b> $hash" : ""; //$h_hash = $hash ? "<p><b>Hash:</b> $hash" : "";
@ -646,13 +644,21 @@ function _fatal_error(\Exception $e): void
print("Message: $message\n"); print("Message: $message\n");
if ($query) { if (is_a($e, DatabaseException::class)) {
print("Query: {$query}\n"); print("Query: {$e->query}\n");
print("Args: ".var_export($e->args, true)."\n");
} }
print("Version: $version (on $phpver)\n"); print("Version: $version (on $phpver)\n");
} else { } else {
$q = $query ? "" : "<p><b>Query:</b> " . 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 .= "<p><b>Query:</b> " . html_escape($query);
$q .= "<p><b>Args:</b> " . html_escape(var_export($e->args, true));
}
if ($code >= 500) { if ($code >= 500) {
error_log("Shimmie Error: $message (Query: $query)\n{$e->getTraceAsString()}"); error_log("Shimmie Error: $message (Query: $query)\n{$e->getTraceAsString()}");
} }

View file

@ -25,10 +25,7 @@ class RandomImage extends Extension
} }
$image = Image::by_random($search_terms); $image = Image::by_random($search_terms);
if (!$image) { if (!$image) {
throw new SCoreException( throw new SCoreException("Couldn't find any posts randomly");
"Couldn't find any posts randomly",
Tag::implode($search_terms)
);
} }
if ($action === "download") { if ($action === "download") {

View file

@ -44,7 +44,7 @@ class TagSetException extends UserErrorException
public function __construct(string $msg, ?string $redirect = null) public function __construct(string $msg, ?string $redirect = null)
{ {
parent::__construct($msg, null); parent::__construct($msg);
$this->redirect = $redirect; $this->redirect = $redirect;
} }
} }