Skip to content

Commit

Permalink
Throw DB connection error in SQL Server for better debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Apr 11, 2021
1 parent 18436cb commit 1dec23f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test-phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ jobs:
steps:
- name: Create database for MSSQL Server
if: matrix.db-platforms == 'SQLSRV'
run: sqlcmd -S 127.0.0.1 -U sa -P 1Secure*Password1 -Q "CREATE DATABASE test"
run: |
if [ ${{ matrix.php-versions == '8.0' }} ]; then
sqlcmd -S localhost,1433 -U sa -P 1Secure*Password1 -Q "CREATE DATABASE test"
else
sqlcmd -S 127.0.0.1,1433 -U sa -P 1Secure*Password1 -Q "CREATE DATABASE test"
fi
- name: Checkout
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion app/Views/errors/html/error_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<!-- Header -->
<div class="header">
<div class="container">
<h1><?= esc($title), esc($exception->getCode() ? ' #' . $exception->getCode() : '') ?></h1>
<h1><?= esc($title) ?></h1>
<p>
<?= nl2br(esc($exception->getMessage())) ?>
<a href="https://www.duckduckgo.com/?q=<?= urlencode($title . ' ' . preg_replace('#\'.*\'|".*"#Us', '', $exception->getMessage())) ?>"
Expand Down
18 changes: 14 additions & 4 deletions system/Database/SQLSRV/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function __construct($params)
/**
* Connect to the database.
*
* @param boolean $persistent
* @param boolean $persistent
*
* @throws DatabaseException
*
* @return mixed
*/
public function connect(bool $persistent = false)
Expand All @@ -127,9 +130,11 @@ public function connect(bool $persistent = false)
unset($connection['UID'], $connection['PWD']);
}

if (false !== ($this->connID = sqlsrv_connect($this->hostname, $connection)))
$this->connID = sqlsrv_connect($this->hostname, $connection);

if ($this->connID !== false)
{
/* Disable warnings as errors behavior. */
// Disable warnings as errors behavior.
sqlsrv_configure('WarningsReturnAsErrors', 0);

// Determine how identifiers are escaped
Expand All @@ -138,9 +143,14 @@ public function connect(bool $persistent = false)

$this->_quoted_identifier = empty($query) ? false : (bool) $query[0]->qi;
$this->escapeChar = ($this->_quoted_identifier) ? '"' : ['[', ']'];

return $this->connID;
}

return $this->connID;
$error = $this->error();
$message = preg_replace('/(\[.+\]\[.+\](?:\[.+\])?)(.+)/', '$2', $error['message']);

throw new DatabaseException($message);
}

/**
Expand Down

0 comments on commit 1dec23f

Please sign in to comment.