drop function: db_connect_with_errors(); just throw from db_connect() if something goes wrong

pull/277/head
David Goodwin 5 years ago
parent 87746e6de8
commit cc19870923

@ -1464,29 +1464,14 @@ EOF;
* @return \PDO
*/
function db_connect() {
list($link, $error_text) = db_connect_with_errors();
if (!$link instanceof PDO) {
throw new Exception("Database connection failed: $error_text");
}
return $link;
}
/**
* @param bool $ignore_errors
* @return array [PDO link | false, string $error_text];
*/
function db_connect_with_errors() {
global $CONF;
global $DEBUG_TEXT;
$error_text = '';
/* some attempt at not reopening an existing connection */
static $link;
if (isset($link) && $link) {
return array($link, $error_text);
return $link;
}
$link = false;
$options = array(
@ -1520,17 +1505,17 @@ function db_connect_with_errors() {
if (!file_exists($db)) {
$error_text = 'SQLite database missing: '. $db;
return array($link, $error_text);
throw new Exception($error_text);
}
if (!is_writeable($db)) {
$error_text = 'SQLite database not writeable: '. $db;
return array($link, $error_text);
throw new Exception($error_text);
}
if (!is_writeable(dirname($db))) {
$error_text = 'The directory the SQLite database is in is not writeable: '. dirname($db);
return array($link, $error_text);
throw new Exception($error_text);
}
$dsn = "sqlite:{$db}";
@ -1556,7 +1541,8 @@ function db_connect_with_errors() {
}
}
return array($link, $error_text);
return $link;
}
/**

@ -184,7 +184,15 @@ if ($f_sqlite_open == 1) {
//
// Database connection
//
list($link, $error_text) = db_connect_with_errors();
$link = null;
$error_text = null;
try {
$link = db_connect();
}
catch(Exception $e) {
$error_text = $e->getMessage();
}
if (!empty($link) && $error_text == "") {
print "<li>Testing database connection (using {$CONF['database_type']}) - Success</li>";

@ -28,7 +28,7 @@ if (getenv('DATABASE') == 'postgresql') {
$user = getenv('PGUSER') ?: 'postgres';
$pass = getenv('PGPASSWORD') ?: '';
$host = getenv('PGHOST') ?: 'localhost';
$CONF['database_type'] = 'pgsql';
$CONF['database_user'] = $user;
$CONF['database_password'] = $pass;
@ -56,7 +56,7 @@ if (getenv('DATABASE') == 'mysql') {
$config = parse_ini_file($expand_tilde('~/.my.cnf'));
if (empty($config)) {
$config = ['user'=>'root', 'host' => '127.0.0.1', 'password' => ''];
$config = ['user' => 'root', 'host' => '127.0.0.1', 'password' => ''];
}
if (isset($config['socket'])) {
@ -79,13 +79,13 @@ if (getenv('DATABASE') == 'mysql') {
echo "Using: MySQL database for tests\n";
}
list($db, $error_text) = db_connect_with_errors();
if ($db === false) {
try {
$db = db_connect();
} catch (Exception $e) {
echo "failed to connect to database\n";
echo $error_text;
echo $e->getMessage();
exit(1);
}
require_once(dirname(__FILE__) . '/../public/upgrade.php');

Loading…
Cancel
Save