3 * This file contains database error classes.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Database error base class
28 class DBError
extends Exception
{
29 /** @var IDatabase|null */
33 * Construct a database error
34 * @param IDatabase $db Object which threw the error
35 * @param string $error A simple error message to be used for debugging
37 function __construct( IDatabase
$db = null, $error ) {
39 parent
::__construct( $error );
44 * Base class for the more common types of database errors. These are known to occur
45 * frequently, so we try to give friendly error messages for them.
50 class DBExpectedError
extends DBError
implements MessageSpecifier
{
51 /** @var string[] Message parameters */
54 function __construct( IDatabase
$db = null, $error, array $params = [] ) {
55 parent
::__construct( $db, $error );
56 $this->params
= $params;
59 public function getKey() {
60 return 'databaseerror-text';
63 public function getParams() {
71 class DBConnectionError
extends DBExpectedError
{
73 * @param IDatabase $db Object throwing the error
74 * @param string $error Error text
76 function __construct( IDatabase
$db = null, $error = 'unknown error' ) {
77 $msg = 'Cannot access the database';
78 if ( trim( $error ) != '' ) {
82 parent
::__construct( $db, $msg );
89 class DBQueryError
extends DBExpectedError
{
100 * @param IDatabase $db
101 * @param string $error
102 * @param int|string $errno
104 * @param string $fname
106 function __construct( IDatabase
$db, $error, $errno, $sql, $fname ) {
107 if ( $db instanceof DatabaseBase
&& $db->wasConnectionError( $errno ) ) {
108 $message = "A connection error occured. \n" .
110 "Function: $fname\n" .
111 "Error: $errno $error\n";
113 $message = "A database error has occurred. Did you forget to run " .
114 "maintenance/update.php after upgrading? See: " .
115 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
117 "Function: $fname\n" .
118 "Error: $errno $error\n";
120 parent
::__construct( $db, $message );
122 $this->error
= $error;
123 $this->errno
= $errno;
125 $this->fname
= $fname;
132 class DBReadOnlyError
extends DBExpectedError
{
138 class DBTransactionError
extends DBExpectedError
{
144 class DBTransactionSizeError
extends DBTransactionError
{
146 return 'transaction-duration-limit-exceeded';
151 * Exception class for replica DB wait timeouts
154 class DBReplicationWaitError
extends DBExpectedError
{
160 class DBUnexpectedError
extends DBError
{
164 * Exception class for attempted DB access
167 class DBAccessError
extends DBUnexpectedError
{
168 public function __construct() {
169 parent
::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
170 "This is not allowed, because database access has been disabled." );