user: Allow "CAS update failed" exceptions to be normalised
[lhc/web/wiklou.git] / includes / exception / MWException.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * MediaWiki exception
23 *
24 * @ingroup Exception
25 */
26 class MWException extends Exception {
27 /**
28 * Should the exception use $wgOut to output the error?
29 *
30 * @return bool
31 */
32 public function useOutputPage() {
33 return $this->useMessageCache() &&
34 !empty( $GLOBALS['wgFullyInitialised'] ) &&
35 !empty( $GLOBALS['wgOut'] ) &&
36 !defined( 'MEDIAWIKI_INSTALL' );
37 }
38
39 /**
40 * Whether to log this exception in the exception debug log.
41 *
42 * @since 1.23
43 * @return bool
44 */
45 public function isLoggable() {
46 return true;
47 }
48
49 /**
50 * Can the extension use the Message class/wfMessage to get i18n-ed messages?
51 *
52 * @return bool
53 */
54 public function useMessageCache() {
55 global $wgLang;
56
57 foreach ( $this->getTrace() as $frame ) {
58 if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
59 return false;
60 }
61 }
62
63 return $wgLang instanceof Language;
64 }
65
66 /**
67 * Get a message from i18n
68 *
69 * @param string $key Message name
70 * @param string $fallback Default message if the message cache can't be
71 * called by the exception
72 * The function also has other parameters that are arguments for the message
73 * @return string Message with arguments replaced
74 */
75 public function msg( $key, $fallback /*[, params...] */ ) {
76 $args = array_slice( func_get_args(), 2 );
77
78 if ( $this->useMessageCache() ) {
79 try {
80 return wfMessage( $key, $args )->text();
81 } catch ( Exception $e ) {
82 }
83 }
84 return wfMsgReplaceArgs( $fallback, $args );
85 }
86
87 /**
88 * If $wgShowExceptionDetails is true, return a HTML message with a
89 * backtrace to the error, otherwise show a message to ask to set it to true
90 * to show that information.
91 *
92 * @return string Html to output
93 */
94 public function getHTML() {
95 global $wgShowExceptionDetails;
96
97 if ( $wgShowExceptionDetails ) {
98 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
99 '</p><p>Backtrace:</p><p>' .
100 nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
101 "</p>\n";
102 } else {
103 $logId = WebRequest::getRequestId();
104 $type = static::class;
105 return Html::errorBox(
106 htmlspecialchars(
107 '[' . $logId . '] ' .
108 gmdate( 'Y-m-d H:i:s' ) . ": " .
109 $this->msg( "internalerror-fatal-exception",
110 "Fatal exception of type $1",
111 $type,
112 $logId,
113 MWExceptionHandler::getURL( $this )
114 )
115 ) ) .
116 "<!-- Set \$wgShowExceptionDetails = true; " .
117 "at the bottom of LocalSettings.php to show detailed " .
118 "debugging information. -->";
119 }
120 }
121
122 /**
123 * Get the text to display when reporting the error on the command line.
124 * If $wgShowExceptionDetails is true, return a text message with a
125 * backtrace to the error.
126 *
127 * @return string
128 */
129 public function getText() {
130 global $wgShowExceptionDetails;
131
132 if ( $wgShowExceptionDetails ) {
133 return MWExceptionHandler::getLogMessage( $this ) .
134 "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
135 } else {
136 return "Set \$wgShowExceptionDetails = true; " .
137 "in LocalSettings.php to show detailed debugging information.\n";
138 }
139 }
140
141 /**
142 * Return the title of the page when reporting this error in a HTTP response.
143 *
144 * @return string
145 */
146 public function getPageTitle() {
147 return $this->msg( 'internalerror', 'Internal error' );
148 }
149
150 /**
151 * Output the exception report using HTML.
152 */
153 public function reportHTML() {
154 global $wgOut, $wgSitename;
155 if ( $this->useOutputPage() ) {
156 $wgOut->prepareErrorPage( $this->getPageTitle() );
157
158 $wgOut->addHTML( $this->getHTML() );
159
160 $wgOut->output();
161 } else {
162 self::header( 'Content-Type: text/html; charset=utf-8' );
163 echo "<!DOCTYPE html>\n" .
164 '<html><head>' .
165 // Mimick OutputPage::setPageTitle behaviour
166 '<title>' .
167 htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
168 '</title>' .
169 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
170 "</head><body>\n";
171
172 echo $this->getHTML();
173
174 echo "</body></html>\n";
175 }
176 }
177
178 /**
179 * Output a report about the exception and takes care of formatting.
180 * It will be either HTML or plain text based on isCommandLine().
181 */
182 public function report() {
183 global $wgMimeType;
184
185 if ( defined( 'MW_API' ) ) {
186 // Unhandled API exception, we can't be sure that format printer is alive
187 self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
188 wfHttpError( 500, 'Internal Server Error', $this->getText() );
189 } elseif ( self::isCommandLine() ) {
190 $message = $this->getText();
191 // T17602: STDERR may not be available
192 if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
193 fwrite( STDERR, $message );
194 } else {
195 echo $message;
196 }
197 } else {
198 self::statusHeader( 500 );
199 self::header( "Content-Type: $wgMimeType; charset=utf-8" );
200
201 $this->reportHTML();
202 }
203 }
204
205 /**
206 * Check whether we are in command line mode or not to report the exception
207 * in the correct format.
208 *
209 * @return bool
210 */
211 public static function isCommandLine() {
212 return !empty( $GLOBALS['wgCommandLineMode'] );
213 }
214
215 /**
216 * Send a header, if we haven't already sent them. We shouldn't,
217 * but sometimes we might in a weird case like Export
218 * @param string $header
219 */
220 private static function header( $header ) {
221 if ( !headers_sent() ) {
222 header( $header );
223 }
224 }
225 private static function statusHeader( $code ) {
226 if ( !headers_sent() ) {
227 HttpStatus::header( $code );
228 }
229 }
230 }