Skip to content

Commit 3b3a68d

Browse files
committed
Improve error output of sql error #1657
1 parent 55b93d8 commit 3b3a68d

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

adm_program/system/classes/Database.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ public function query(string $sql, bool $showError = true)
735735

736736
if ($showError) {
737737
$gLogger->critical('PDOException: ' . $exception->getMessage());
738-
$this->showError();
738+
$this->showError($exception->getMessage(), $exception->getCode());
739739
// => EXIT
740740
}
741741

@@ -794,7 +794,7 @@ public function queryPrepared(string $sql, array $params = array(), bool $showEr
794794

795795
if ($showError) {
796796
$gLogger->critical('PDOException: ' . $exception->getMessage());
797-
$this->showError();
797+
$this->showError($exception->getMessage(), $exception->getCode());
798798
// => EXIT
799799
}
800800

@@ -929,31 +929,32 @@ private function setConnectionOptions()
929929
* The error must be read by the child method. This method will call a backtrace, so
930930
* you see the script and specific line in which the error occurred.
931931
* @param string $errorMessage Optional an error message could be set and integrated in the output of the sql error.
932+
* @param int $code Optional a code for the error be set and integrated in the output of the sql error.
932933
* @return void Will exit the script and returns a html output with the error information.
933934
* @throws Exception
934935
*/
935-
public function showError(string $errorMessage = '')
936+
public function showError(string $errorMessage = '', int $code = 0)
936937
{
937938
global $gLogger, $gSettingsManager, $gL10n;
938939

940+
if ($errorMessage === '') {
941+
// transform the database error to html
942+
$code = $this->pdo->errorCode();
943+
$errorMessage = $this->pdo->errorInfo()[2];
944+
}
945+
939946
// Rollback on open transaction
940947
if ($this->transactions > 0) {
941948
$this->pdo->rollBack();
942949
}
943950

944-
// transform the database error to html
945-
$errorCode = $this->pdo->errorCode();
946-
$errorInfo = $this->pdo->errorInfo();
947-
948-
$gLogger->critical($errorCode.': '.$errorInfo[1].' | '.$errorInfo[2]);
951+
$gLogger->critical($code . ': ' . $errorMessage);
949952

950953
$htmlOutput = '
951954
<div style="font-family: monospace;">
952955
<p><strong>S Q L - E R R O R</strong></p>
953-
' . $errorMessage . '
954-
<p><strong>CODE:</strong> ' . $errorCode . '</p>
955-
' . $errorInfo[1] . '<br /><br />
956-
' . $errorInfo[2] . '<br /><br />
956+
<p>' . $errorMessage . '</p>
957+
<p><strong>CODE:</strong> ' . $code . '</p>
957958
<strong>B A C K T R A C E</strong><br />
958959
' . $this->getBacktrace() . '
959960
</div>';

0 commit comments

Comments
 (0)