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.
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.
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
26 class MWException
extends Exception
{
28 * Should the exception use $wgOut to output the error?
32 public function useOutputPage() {
33 return $this->useMessageCache() &&
34 !empty( $GLOBALS['wgFullyInitialised'] ) &&
35 !empty( $GLOBALS['wgOut'] ) &&
36 !defined( 'MEDIAWIKI_INSTALL' );
40 * Whether to log this exception in the exception debug log.
45 public function isLoggable() {
50 * Can the extension use the Message class/wfMessage to get i18n-ed messages?
54 public function useMessageCache() {
57 foreach ( $this->getTrace() as $frame ) {
58 if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache
::class ) {
63 return $wgLang instanceof Language
;
67 * Get a message from i18n
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 * @param mixed ...$params To pass to wfMessage()
73 * @return string Message with arguments replaced
75 public function msg( $key, $fallback, ...$params ) {
78 // FIXME: Keep logic in sync with MWExceptionRenderer::msg.
80 if ( $this->useMessageCache() ) {
82 $res = wfMessage( $key, $params )->text();
83 } catch ( Exception
$e ) {
86 if ( $res === false ) {
87 $res = wfMsgReplaceArgs( $fallback, $params );
88 // If an exception happens inside message rendering,
89 // {{SITENAME}} sometimes won't be replaced.
91 '{{SITENAME}}' => $wgSitename,
98 * If $wgShowExceptionDetails is true, return a HTML message with a
99 * backtrace to the error, otherwise show a message to ask to set it to true
100 * to show that information.
102 * @return string Html to output
104 public function getHTML() {
105 global $wgShowExceptionDetails;
107 if ( $wgShowExceptionDetails ) {
108 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler
::getLogMessage( $this ) ) ) .
109 '</p><p>Backtrace:</p><p>' .
110 nl2br( htmlspecialchars( MWExceptionHandler
::getRedactedTraceAsString( $this ) ) ) .
113 $logId = WebRequest
::getRequestId();
114 $type = static::class;
115 return Html
::errorBox(
117 '[' . $logId . '] ' .
118 gmdate( 'Y-m-d H:i:s' ) . ": " .
119 $this->msg( "internalerror-fatal-exception",
120 "Fatal exception of type $1",
123 MWExceptionHandler
::getURL()
126 "<!-- Set \$wgShowExceptionDetails = true; " .
127 "at the bottom of LocalSettings.php to show detailed " .
128 "debugging information. -->";
133 * Get the text to display when reporting the error on the command line.
134 * If $wgShowExceptionDetails is true, return a text message with a
135 * backtrace to the error.
139 public function getText() {
140 global $wgShowExceptionDetails;
142 if ( $wgShowExceptionDetails ) {
143 return MWExceptionHandler
::getLogMessage( $this ) .
144 "\nBacktrace:\n" . MWExceptionHandler
::getRedactedTraceAsString( $this ) . "\n";
146 return "Set \$wgShowExceptionDetails = true; " .
147 "in LocalSettings.php to show detailed debugging information.\n";
152 * Return the title of the page when reporting this error in a HTTP response.
156 public function getPageTitle() {
157 return $this->msg( 'internalerror', 'Internal error' );
161 * Output the exception report using HTML.
163 public function reportHTML() {
164 global $wgOut, $wgSitename;
165 if ( $this->useOutputPage() ) {
166 $wgOut->prepareErrorPage( $this->getPageTitle() );
167 // Manually set the html title, since sometimes
168 // {{SITENAME}} does not get replaced for exceptions
169 // happening inside message rendering.
170 $wgOut->setHTMLTitle(
174 $this->getPageTitle()
178 $wgOut->addHTML( $this->getHTML() );
182 self
::header( 'Content-Type: text/html; charset=utf-8' );
183 echo "<!DOCTYPE html>\n" .
185 // Mimick OutputPage::setPageTitle behaviour
187 htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
189 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
192 echo $this->getHTML();
194 echo "</body></html>\n";
199 * Output a report about the exception and takes care of formatting.
200 * It will be either HTML or plain text based on isCommandLine().
202 public function report() {
205 if ( defined( 'MW_API' ) ) {
206 // Unhandled API exception, we can't be sure that format printer is alive
207 self
::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
208 wfHttpError( 500, 'Internal Server Error', $this->getText() );
209 } elseif ( self
::isCommandLine() ) {
210 $message = $this->getText();
211 $this->writeToCommandLine( $message );
213 self
::statusHeader( 500 );
214 self
::header( "Content-Type: $wgMimeType; charset=utf-8" );
221 * Write a message to stderr falling back to stdout if stderr unavailable
223 * @param string $message
224 * @suppress SecurityCheck-XSS
226 private function writeToCommandLine( $message ) {
227 // T17602: STDERR may not be available
228 if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
229 fwrite( STDERR
, $message );
236 * Check whether we are in command line mode or not to report the exception
237 * in the correct format.
241 public static function isCommandLine() {
242 return !empty( $GLOBALS['wgCommandLineMode'] );
246 * Send a header, if we haven't already sent them. We shouldn't,
247 * but sometimes we might in a weird case like Export
248 * @param string $header
250 private static function header( $header ) {
251 if ( !headers_sent() ) {
256 private static function statusHeader( $code ) {
257 if ( !headers_sent() ) {
258 HttpStatus
::header( $code );