html_escape all exception messages - pass query out-of-band if we want it formatted

This commit is contained in:
Shish 2019-11-11 16:43:04 +00:00
parent 6486bb95da
commit c94f289291
4 changed files with 20 additions and 20 deletions

View file

@ -17,7 +17,7 @@ class Database
* @var null|PDO * @var null|PDO
*/ */
private $db = null; private $db = null;
/** /**
* @var float * @var float
*/ */
@ -103,29 +103,21 @@ class Database
public function commit(): bool public function commit(): bool
{ {
if (!is_null($this->db)) { if (!is_null($this->db) && $this->transaction === true) {
if ($this->transaction === true) { $this->transaction = false;
$this->transaction = false; return $this->db->commit();
return $this->db->commit();
} else {
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call commit() as there is no transaction currently open.");
}
} else { } else {
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call commit() as there is no connection currently open."); throw new SCoreException("Unable to call commit() as there is no transaction currently open.");
} }
} }
public function rollback(): bool public function rollback(): bool
{ {
if (!is_null($this->db)) { if (!is_null($this->db) && $this->transaction === true) {
if ($this->transaction === true) { $this->transaction = false;
$this->transaction = false; return $this->db->rollback();
return $this->db->rollback();
} else {
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call rollback() as there is no transaction currently open.");
}
} else { } else {
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call rollback() as there is no connection currently open."); throw new SCoreException("Unable to call rollback() as there is no transaction currently open.");
} }
} }
@ -213,7 +205,7 @@ class Database
} }
return $stmt; return $stmt;
} catch (PDOException $pdoe) { } catch (PDOException $pdoe) {
throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query); throw new SCoreException($pdoe->getMessage(), $query);
} }
} }

View file

@ -7,6 +7,11 @@
*/ */
class SCoreException extends Exception class SCoreException extends Exception
{ {
public function __construct(string $msg, ?string $query=null)
{
parent::__construct($msg);
$this->query = $query;
}
} }
/** /**

View file

@ -63,7 +63,7 @@ class UserClass
$min_ability = $a; $min_ability = $a;
} }
} }
throw new SCoreException("Unknown ability '".html_escape($ability)."'. Did the developer mean '".html_escape($min_ability)."'?"); throw new SCoreException("Unknown ability '$ability'. Did the developer mean '$min_ability'?");
} }
} }
} }

View file

@ -522,6 +522,8 @@ function _fatal_error(Exception $e): void
//$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" : "";
//'.$h_hash.' //'.$h_hash.'
$q = (!isset($e->query) || is_null($e->query)) ? "" : "<p><b>Query:</b> " . html_escape($e->query);
header("HTTP/1.0 500 Internal Error"); header("HTTP/1.0 500 Internal Error");
echo ' echo '
@ -531,7 +533,8 @@ function _fatal_error(Exception $e): void
</head> </head>
<body> <body>
<h1>Internal Error</h1> <h1>Internal Error</h1>
<p><b>Message:</b> '.$message.' <p><b>Message:</b> '.html_escape($message).'
'.$q.'
<p><b>Version:</b> '.$version.' (on '.phpversion().') <p><b>Version:</b> '.$version.' (on '.phpversion().')
</body> </body>
</html> </html>