Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Velluppillai <justin@innocraft.com>2021-07-19 21:21:20 +0300
committerGitHub <noreply@github.com>2021-07-19 21:21:20 +0300
commit9aea15489e3a4bd237933e089daaae64722efec2 (patch)
tree7decef062118cfec6cccc64b1df991de968ade79
parentde484d49afbf14b538add8ccb4bae37d0d3f5d81 (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
-rw-r--r--core/Db/Adapter.php47
-rw-r--r--lang/en.json2
-rw-r--r--plugins/Installation/tests/System/APITest.php4
-rw-r--r--tests/PHPUnit/Unit/Tracker/CacheTest.php8
-rw-r--r--tests/UI/expected-screenshots/UIIntegrationTest_db_connect_error.png4
5 files changed, 54 insertions, 11 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 '';
+ }
}
diff --git a/lang/en.json b/lang/en.json
index f71889a9b1..de2344a89f 100644
--- a/lang/en.json
+++ b/lang/en.json
@@ -177,6 +177,8 @@
"ExceptionDatabaseVersion": "Your %1$s version is %2$s but Matomo requires at least %3$s.",
"ExceptionDatabaseVersionNewerThanCodebase": "Your Matomo codebase is running the old version %1$s and we have detected that your Matomo Database has already been upgraded to the newer version %2$s.",
"ExceptionDatabaseVersionNewerThanCodebaseWait": "Maybe your Matomo administrators are currently finishing the upgrade process. Please try again in a few minutes.",
+ "ExceptionDatabaseUnavailable": "MySQL server has gone away",
+ "ExceptionDatabaseAccess": "Database access denied",
"ExceptionFileIntegrity": "Integrity check failed: %s",
"ExceptionFilesizeMismatch": "File size mismatch: %1$s (expected length: %2$s, found: %3$s)",
"ExceptionIncompatibleClientServerVersions": "Your %1$s client version is %2$s which is incompatible with server version %3$s.",
diff --git a/plugins/Installation/tests/System/APITest.php b/plugins/Installation/tests/System/APITest.php
index faca839ee0..370b99e3bc 100644
--- a/plugins/Installation/tests/System/APITest.php
+++ b/plugins/Installation/tests/System/APITest.php
@@ -47,7 +47,7 @@ class APITest extends SystemTestCase
$data = str_replace("\n", "", $response['data']);
$this->assertStringStartsWith('<?xml version="1.0" encoding="utf-8" ?><result> <error message=', $data);
- self::assertStringContainsString('Access denied', $data);
+ self::assertStringContainsString('Database access denied', $data);
$this->assertStringEndsWith('</result>', $data);
}
@@ -58,7 +58,7 @@ class APITest extends SystemTestCase
$data = str_replace("\n", "", $response['data']);
$this->assertStringStartsWith('{"result":"error","message":"', $data);
- self::assertStringContainsString('Access denied', $data);
+ self::assertStringContainsString('Database access denied', $data);
}
public function test_shouldReturnEmptyResultWhenNotInstalledAndDispatchIsDisabled()
diff --git a/tests/PHPUnit/Unit/Tracker/CacheTest.php b/tests/PHPUnit/Unit/Tracker/CacheTest.php
index 145cd56004..0408cff6d6 100644
--- a/tests/PHPUnit/Unit/Tracker/CacheTest.php
+++ b/tests/PHPUnit/Unit/Tracker/CacheTest.php
@@ -54,7 +54,7 @@ class CacheTest extends UnitTestCase
public function provideContainerConfig()
{
$mockLazyCache = $this->getMockBuilder(Lazy::class)
- ->onlyMethods(['flushAll', 'delete'])
+ ->onlyMethods(['flushAll', 'fetch', 'save', 'delete'])
->disableOriginalConstructor()
->getMock();
$mockLazyCache
@@ -64,6 +64,12 @@ class CacheTest extends UnitTestCase
$mockLazyCache->method('delete')->willReturnCallback(function ($key) {
$this->methodsCalled[] = 'delete.' . $key;
});
+ $mockLazyCache->method('save')->willReturnCallback(function ($id, $data) {
+ $this->methodsCalled[] = 'save.' . $id . $data;
+ });
+ $mockLazyCache->method('fetch')->willReturnCallback(function ($id) {
+ $this->methodsCalled[] = 'fetch.' . $id;
+ });
return [
Lazy::class => $mockLazyCache,
diff --git a/tests/UI/expected-screenshots/UIIntegrationTest_db_connect_error.png b/tests/UI/expected-screenshots/UIIntegrationTest_db_connect_error.png
index 066a46245c..952dc6e7d6 100644
--- a/tests/UI/expected-screenshots/UIIntegrationTest_db_connect_error.png
+++ b/tests/UI/expected-screenshots/UIIntegrationTest_db_connect_error.png
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:1c66b67f954aaa21a5ef577189220cd2018e210cdbbda219a9af0adae1698634
-size 75052
+oid sha256:67b00cbec9103edce15aee9ba01ff977f285063434b5d9f6dba66efc0fb407e5
+size 62482