diff options
author | Justin Velluppillai <justin@innocraft.com> | 2021-07-19 21:21:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-19 21:21:20 +0300 |
commit | 9aea15489e3a4bd237933e089daaae64722efec2 (patch) | |
tree | 7decef062118cfec6cccc64b1df991de968ade79 /core | |
parent | de484d49afbf14b538add8ccb4bae37d0d3f5d81 (diff) |
More helpful messages on db connection failures (#17789)
* Added a message map to allow translating db error messages
* Allow translation of db error message
* Also include mysql error numbers as possible detection mechanisms for errors
* Only create a new exception if we're changing the message
* Missed change
* Fixing tests
* More test fixes
* Update en.json
Use MySQL server has gone away to help people google the fix
Diffstat (limited to 'core')
-rw-r--r-- | core/Db/Adapter.php | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/core/Db/Adapter.php b/core/Db/Adapter.php index 7beccd9c24..44263a9f50 100644 --- a/core/Db/Adapter.php +++ b/core/Db/Adapter.php @@ -9,6 +9,7 @@ namespace Piwik\Db; use Zend_Db_Table; +use Piwik\Piwik; /** */ @@ -45,14 +46,24 @@ class Adapter $infos[$key] = $val; } - $adapter = new $className($infos); + $adapter = new $className($infos); if ($connect) { - $adapter->getConnection(); - - Zend_Db_Table::setDefaultAdapter($adapter); - // we don't want the connection information to appear in the logs - $adapter->resetConfig(); + try { + $adapter->getConnection(); + + Zend_Db_Table::setDefaultAdapter($adapter); + // we don't want the connection information to appear in the logs + $adapter->resetConfig(); + } catch(\Exception $e) { + // we don't want certain exceptions to leak information + $msg = self::overriddenExceptionMessage($e->getMessage()); + if ('' !== $msg) { + throw new \Exception($msg); + } + + throw $e; + } } return $adapter; @@ -129,4 +140,28 @@ class Adapter { return strtolower($adapterName) === 'pdo/mysql'; } + + /** + * Intercepts certain exception messages and replaces leaky ones with ones that don't reveal too much info + * @param string $message + * @return string + */ + public static function overriddenExceptionMessage($message) + { + $safeMessageMap = array( + // add any exception search terms and their replacement message here + '[2006]' => Piwik::translate('General_ExceptionDatabaseUnavailable'), + 'MySQL server has gone away' => Piwik::translate('General_ExceptionDatabaseUnavailable'), + '[1698]' => Piwik::translate('General_ExceptionDatabaseAccess'), + 'Access denied' => Piwik::translate('General_ExceptionDatabaseAccess') + ); + + foreach ($safeMessageMap as $search_term => $safeMessage) { + if (strpos($message, $search_term) !== false) { + return $safeMessage; + } + } + + return ''; + } } |