Merge "Deprecate jquery.arrowSteps module"
[lhc/web/wiklou.git] / includes / libs / rdbms / exception / DBError.php
1 <?php
2 /**
3 * This file contains database error classes.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database error base class
26 * @ingroup Database
27 */
28 class DBError extends Exception {
29 /** @var IDatabase|null */
30 public $db;
31
32 /**
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
36 */
37 function __construct( IDatabase $db = null, $error ) {
38 $this->db = $db;
39 parent::__construct( $error );
40 }
41 }
42
43 /**
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.
46 *
47 * @ingroup Database
48 * @since 1.23
49 */
50 class DBExpectedError extends DBError implements MessageSpecifier {
51 /** @var string[] Message parameters */
52 protected $params;
53
54 function __construct( IDatabase $db = null, $error, array $params = [] ) {
55 parent::__construct( $db, $error );
56 $this->params = $params;
57 }
58
59 public function getKey() {
60 return 'databaseerror-text';
61 }
62
63 public function getParams() {
64 return $this->params;
65 }
66 }
67
68 /**
69 * @ingroup Database
70 */
71 class DBConnectionError extends DBExpectedError {
72 /**
73 * @param IDatabase $db Object throwing the error
74 * @param string $error Error text
75 */
76 function __construct( IDatabase $db = null, $error = 'unknown error' ) {
77 $msg = 'Cannot access the database';
78 if ( trim( $error ) != '' ) {
79 $msg .= ": $error";
80 }
81
82 parent::__construct( $db, $msg );
83 }
84 }
85
86 /**
87 * @ingroup Database
88 */
89 class DBQueryError extends DBExpectedError {
90 /** @var string */
91 public $error;
92 /** @var integer */
93 public $errno;
94 /** @var string */
95 public $sql;
96 /** @var string */
97 public $fname;
98
99 /**
100 * @param IDatabase $db
101 * @param string $error
102 * @param int|string $errno
103 * @param string $sql
104 * @param string $fname
105 */
106 function __construct( IDatabase $db, $error, $errno, $sql, $fname ) {
107 if ( $db instanceof DatabaseBase && $db->wasConnectionError( $errno ) ) {
108 $message = "A connection error occured. \n" .
109 "Query: $sql\n" .
110 "Function: $fname\n" .
111 "Error: $errno $error\n";
112 } else {
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" .
116 "Query: $sql\n" .
117 "Function: $fname\n" .
118 "Error: $errno $error\n";
119 }
120 parent::__construct( $db, $message );
121
122 $this->error = $error;
123 $this->errno = $errno;
124 $this->sql = $sql;
125 $this->fname = $fname;
126 }
127 }
128
129 /**
130 * @ingroup Database
131 */
132 class DBReadOnlyError extends DBExpectedError {
133 }
134
135 /**
136 * @ingroup Database
137 */
138 class DBTransactionError extends DBExpectedError {
139 }
140
141 /**
142 * @ingroup Database
143 */
144 class DBTransactionSizeError extends DBTransactionError {
145 function getKey() {
146 return 'transaction-duration-limit-exceeded';
147 }
148 }
149
150 /**
151 * Exception class for replica DB wait timeouts
152 * @ingroup Database
153 */
154 class DBReplicationWaitError extends DBExpectedError {
155 }
156
157 /**
158 * @ingroup Database
159 */
160 class DBUnexpectedError extends DBError {
161 }
162
163 /**
164 * Exception class for attempted DB access
165 * @ingroup Database
166 */
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." );
171 }
172 }
173