Gergő Tisza has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/367673 )

Change subject: Make DBQueryError message handling more useful
......................................................................

Make DBQueryError message handling more useful

DBQueryError implements ILocalizedException in a particularly
useless way, which results in ILocalizedException-aware
error handlers (e.g. the action API) hiding all exception details.
Make it return a localizable version of the exception message instead.
The formatting will still look ugly but at least developers will have
a fighting chance of figuring out what's going on.

Also offer a little more guidance on how ILocalizedException should
be implemented.

Change-Id: If5fb4ccfe37cda22489f4b4972fe29fd5842b146
---
M includes/exception/LocalizedException.php
M includes/libs/rdbms/exception/DBQueryError.php
M languages/i18n/en.json
M languages/i18n/qqq.json
4 files changed, 38 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/73/367673/1

diff --git a/includes/exception/LocalizedException.php 
b/includes/exception/LocalizedException.php
index d2cb5d1..ebd2387 100644
--- a/includes/exception/LocalizedException.php
+++ b/includes/exception/LocalizedException.php
@@ -26,7 +26,9 @@
  */
 interface ILocalizedException {
        /**
-        * Return a Message object for this exception
+        * Return a Message object for this exception. This will be used in 
place of the exception
+        * message (ie. Exception::getMessage()) by language-aware handlers, 
and is expected to
+        * contain the same information.
         * @return Message
         */
        public function getMessageObject();
diff --git a/includes/libs/rdbms/exception/DBQueryError.php 
b/includes/libs/rdbms/exception/DBQueryError.php
index bc2a865..27a3e07 100644
--- a/includes/libs/rdbms/exception/DBQueryError.php
+++ b/includes/libs/rdbms/exception/DBQueryError.php
@@ -21,6 +21,8 @@
 
 namespace Wikimedia\Rdbms;
 
+use Message;
+
 /**
  * @ingroup Database
  */
@@ -34,6 +36,9 @@
        /** @var string */
        public $fname;
 
+       /** @var Message */
+       private $messageObject;
+
        /**
         * @param IDatabase $db
         * @param string $error
@@ -42,19 +47,12 @@
         * @param string $fname
         */
        function __construct( IDatabase $db, $error, $errno, $sql, $fname ) {
-               if ( $db instanceof Database && $db->wasConnectionError( $errno 
) ) {
-                       $message = "A connection error occured. \n" .
-                               "Query: $sql\n" .
-                               "Function: $fname\n" .
-                               "Error: $errno $error\n";
-               } else {
-                       $message = "A database query error has occurred. Did 
you forget to run " .
-                               "your application's database schema updater 
after upgrading? \n" .
-                               "Query: $sql\n" .
-                               "Function: $fname\n" .
-                               "Error: $errno $error\n";
-               }
+               $key = ( $db instanceof Database && $db->wasConnectionError( 
$errno ) )
+                       ? 'databaseerror-connection' : 'databaseerror-real';
+               $this->messageObject = new Message( $key, [ $sql, $fname, 
$errno, $error ] );
 
+               $message = Message::newFromSpecifier( $this->messageObject 
)->useDatabase( false )
+                       ->inLanguage( 'en' )->plain();
                parent::__construct( $db, $message );
 
                $this->error = $error;
@@ -62,6 +60,27 @@
                $this->sql = $sql;
                $this->fname = $fname;
        }
+
+       /**
+        * @inheritDoc
+        */
+       public function getKey() {
+               return $this->messageObject->getKey();
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function getParams() {
+               return $this->messageObject->getParams();
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function getMessageObject() {
+               return $this->messageObject;
+       }
 }
 
 class_alias( DBQueryError::class, 'DBQueryError' );
diff --git a/languages/i18n/en.json b/languages/i18n/en.json
index 4d51c9e..7fdc792 100644
--- a/languages/i18n/en.json
+++ b/languages/i18n/en.json
@@ -309,6 +309,8 @@
        "databaseerror-query": "Query: $1",
        "databaseerror-function": "Function: $1",
        "databaseerror-error": "Error: $1",
+       "databaseerror-connection": "A connection error occured.\nQuery: 
$1\nFunction: $2\nError: $3 $4\n",
+       "databaseerror-real": "A database query error has occurred. Did you 
forget to run your application's database schema updater after 
upgrading?\nQuery: $1\nFunction: $2\nError: $3 $4\n",
        "transaction-duration-limit-exceeded": "To avoid creating high 
replication lag, this transaction was aborted because the write duration ($1) 
exceeded the $2 second limit.\nIf you are changing many items at once, try 
doing multiple smaller operations instead.",
        "laggedslavemode": "<strong>Warning:</strong> Page may not contain 
recent updates.",
        "readonly": "Database locked",
diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json
index 44335fc..06a2ba0 100644
--- a/languages/i18n/qqq.json
+++ b/languages/i18n/qqq.json
@@ -499,6 +499,8 @@
        "databaseerror-query": "Identifies, in the list of technical details, 
the [[wikipedia:SQL|SQL]] statement that failed.\nParameters:\n* $1 - SQL 
statement (shown within a box)\n{{Identical|Query}}",
        "databaseerror-function": "Identifies, in the list of technical 
details, the function that tried to execute the database query.\nParameters:\n* 
$1 - Name of function\n{{Identical|Function}}",
        "databaseerror-error": "Identifies, in the list of technical details, 
the error message the database server returned.\nParameters:\n* $1 - Error 
message from the database server, probably in English\n{{Identical|Error}}",
+       "databaseerror-connection": "Describes a connection error. 
Parameters:\n* $1 - the SQL query\n* $2 - the name of the method where the 
error occurred\n* $3 - error code, as returned by the database\n* $4 - error 
message, as returned by the database (probably in English)",
+       "databaseerror-real": "Describes a database error. Parameters:\n* $1 - 
the SQL query\n* $2 - the name of the method where the error occurred\n* $3 - 
error code, as returned by the database\n* $4 - error message, as returned by 
the database (probably in English)",
        "transaction-duration-limit-exceeded": "Plain text error shown when DB 
updates take too long. Parameters:\n* $1 - time spent in database updates\n* $2 
- maximum number of seconds allowed in database updates",
        "laggedslavemode": "Used as warning when getting the timestamp of the 
latest version, if in LaggedSlaveMode.",
        "readonly": "Used as title of error message when database is locked.",

-- 
To view, visit https://gerrit.wikimedia.org/r/367673
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If5fb4ccfe37cda22489f4b4972fe29fd5842b146
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: GergÅ‘ Tisza <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to