Skip to content

Commit 275982b

Browse files
committed
detect and report database errors
1 parent a01746b commit 275982b

6 files changed

Lines changed: 35 additions & 11 deletions

File tree

userfrosting/initialize.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class_alias("UserFrosting\MySqlSiteSettings", "UserFrosting\SiteSettings");
139139
]
140140
];
141141

142+
// Load site settings
142143
$app->site = new \UserFrosting\SiteSettings($setting_values, $setting_descriptions);
143144

144145
// Store to DB if not consistent
@@ -188,7 +189,7 @@ class_alias("UserFrosting\MySqlSiteSettings", "UserFrosting\SiteSettings");
188189

189190
// Custom error-handler: send a generic message to the client, but put the specific error info in the error log.
190191
// A Slim application uses its built-in error handler if its debug setting is true; otherwise, it uses the custom error handler.
191-
$app->error(function (\Exception $e) use ($app) {
192+
$app->error(function (\Exception $e) use ($app) {
192193
if ($app->alerts && is_object($app->alerts) && $app->translator)
193194
$app->alerts->addMessageTranslated("danger", "SERVER_ERROR");
194195
error_log("Error in " . $e->getFile() . " on line " . $e->getLine() . ": " . $e->getMessage());
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace UserFrosting;
4+
5+
/**
6+
* Invalid Database Exception.
7+
*/
8+
class DatabaseInvalidException extends \Exception {
9+
/**
10+
* Public constructor.
11+
*/
12+
public function __construct($message, $code, $previous)
13+
{
14+
$message = 'The database is in an invalid state: ' . $message;
15+
parent::__construct($message, 500, $previous);
16+
}
17+
}

userfrosting/models/mysql/MySqlDatabase.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ public static function connection(){
2020
$db_host = static::$app->config('db')['db_host'];
2121
$db_name = static::$app->config('db')['db_name'];
2222

23-
$db = new \PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8", static::$app->config('db')['db_user'], static::$app->config('db')['db_pass']);
24-
$db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
25-
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Let this function throw a PDO exception if it cannot connect.
26-
return $db;
23+
try {
24+
$db = new \PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8", static::$app->config('db')['db_user'], static::$app->config('db')['db_pass']);
25+
$db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
26+
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Let this function throw a PDO exception if it cannot connect.
27+
return $db;
28+
} catch (\PDOException $e){
29+
echo "We can't seem to connect to the database! Please check your database credentials in config-userfrosting.php.";
30+
//throw new DatabaseInvalidException($e->getMessage(), $e->getStatus(), $e->getPrevious());
31+
}
2732
}
2833

2934
/**

userfrosting/vendor/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
require_once __DIR__ . '/composer' . '/autoload_real.php';
66

7-
return ComposerAutoloaderInitd055b96a2ee195553d40749aab9168c6::getLoader();
7+
return ComposerAutoloaderInit153b8801bb6221bd4c1eaa8fb1197815::getLoader();

userfrosting/vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'UserFrosting\\AuthorizationException' => $baseDir . '/auth/AuthorizationException.php',
3131
'UserFrosting\\BaseController' => $baseDir . '/controllers/BaseController.php',
3232
'UserFrosting\\DatabaseInterface' => $baseDir . '/models/DatabaseInterface.php',
33+
'UserFrosting\\DatabaseInvalidException' => $baseDir . '/models/DatabaseInvalidException.php',
3334
'UserFrosting\\DatabaseObjectInterface' => $baseDir . '/models/DatabaseInterface.php',
3435
'UserFrosting\\DatabaseTable' => $baseDir . '/models/DatabaseTable.php',
3536
'UserFrosting\\GroupController' => $baseDir . '/controllers/GroupController.php',

userfrosting/vendor/composer/autoload_real.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// autoload_real.php @generated by Composer
44

5-
class ComposerAutoloaderInitd055b96a2ee195553d40749aab9168c6
5+
class ComposerAutoloaderInit153b8801bb6221bd4c1eaa8fb1197815
66
{
77
private static $loader;
88

@@ -19,9 +19,9 @@ public static function getLoader()
1919
return self::$loader;
2020
}
2121

22-
spl_autoload_register(array('ComposerAutoloaderInitd055b96a2ee195553d40749aab9168c6', 'loadClassLoader'), true, true);
22+
spl_autoload_register(array('ComposerAutoloaderInit153b8801bb6221bd4c1eaa8fb1197815', 'loadClassLoader'), true, true);
2323
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
24-
spl_autoload_unregister(array('ComposerAutoloaderInitd055b96a2ee195553d40749aab9168c6', 'loadClassLoader'));
24+
spl_autoload_unregister(array('ComposerAutoloaderInit153b8801bb6221bd4c1eaa8fb1197815', 'loadClassLoader'));
2525

2626
$map = require __DIR__ . '/autoload_namespaces.php';
2727
foreach ($map as $namespace => $path) {
@@ -42,14 +42,14 @@ public static function getLoader()
4242

4343
$includeFiles = require __DIR__ . '/autoload_files.php';
4444
foreach ($includeFiles as $file) {
45-
composerRequired055b96a2ee195553d40749aab9168c6($file);
45+
composerRequire153b8801bb6221bd4c1eaa8fb1197815($file);
4646
}
4747

4848
return $loader;
4949
}
5050
}
5151

52-
function composerRequired055b96a2ee195553d40749aab9168c6($file)
52+
function composerRequire153b8801bb6221bd4c1eaa8fb1197815($file)
5353
{
5454
require $file;
5555
}

0 commit comments

Comments
 (0)