Add MWExceptionRenderer class and decouple DBError
[lhc/web/wiklou.git] / includes / db / DatabaseError.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 */
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 {
51 }
52
53 /**
54 * @ingroup Database
55 */
56 class DBConnectionError extends DBExpectedError {
57 /**
58 * @param IDatabase $db Object throwing the error
59 * @param string $error Error text
60 */
61 function __construct( IDatabase $db = null, $error = 'unknown error' ) {
62 $msg = 'Cannot access the database';
63 if ( trim( $error ) != '' ) {
64 $msg .= ": $error";
65 }
66
67 parent::__construct( $db, $msg );
68 }
69 }
70
71 /**
72 * @ingroup Database
73 */
74 class DBQueryError extends DBExpectedError {
75 /** @var string */
76 public $error;
77 /** @var integer */
78 public $errno;
79 /** @var string */
80 public $sql;
81 /** @var string */
82 public $fname;
83
84 /**
85 * @param IDatabase $db
86 * @param string $error
87 * @param int|string $errno
88 * @param string $sql
89 * @param string $fname
90 */
91 function __construct( IDatabase $db, $error, $errno, $sql, $fname ) {
92 if ( $db instanceof DatabaseBase && $db->wasConnectionError( $errno ) ) {
93 $message = "A connection error occured. \n" .
94 "Query: $sql\n" .
95 "Function: $fname\n" .
96 "Error: $errno $error\n";
97 } else {
98 $message = "A database error has occurred. Did you forget to run " .
99 "maintenance/update.php after upgrading? See: " .
100 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
101 "Query: $sql\n" .
102 "Function: $fname\n" .
103 "Error: $errno $error\n";
104 }
105 parent::__construct( $db, $message );
106
107 $this->error = $error;
108 $this->errno = $errno;
109 $this->sql = $sql;
110 $this->fname = $fname;
111 }
112 }
113
114 /**
115 * @ingroup Database
116 */
117 class DBReadOnlyError extends DBExpectedError {
118 }
119
120 /**
121 * @ingroup Database
122 */
123 class DBTransactionError extends DBExpectedError {
124 }
125
126 /**
127 * Exception class for replica DB wait timeouts
128 * @ingroup Database
129 */
130 class DBReplicationWaitError extends DBExpectedError {
131 }
132
133 /**
134 * @ingroup Database
135 */
136 class DBUnexpectedError extends DBError {
137 }
138
139 /**
140 * Exception class for attempted DB access
141 * @ingroup Database
142 */
143 class DBAccessError extends DBUnexpectedError {
144 public function __construct() {
145 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
146 "This is not allowed, because database access has been disabled." );
147 }
148 }
149