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 * The function also has other parameters that are arguments for the message
73 * @return string Message with arguments replaced
75 public function msg( $key, $fallback /*[, params...] */ ) {
76 $args = array_slice( func_get_args(), 2 );
78 if ( $this->useMessageCache() ) {
80 return wfMessage( $key, $args )->text();
81 } catch ( Exception
$e ) {
84 return wfMsgReplaceArgs( $fallback, $args );
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.
92 * @return string Html to output
94 public function getHTML() {
95 global $wgShowExceptionDetails;
97 if ( $wgShowExceptionDetails ) {
98 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler
::getLogMessage( $this ) ) ) .
99 '</p><p>Backtrace:</p><p>' .
100 nl2br( htmlspecialchars( MWExceptionHandler
::getRedactedTraceAsString( $this ) ) ) .
103 $logId = WebRequest
::getRequestId();
104 $type = static::class;
105 return Html
::errorBox(
107 '[' . $logId . '] ' .
108 gmdate( 'Y-m-d H:i:s' ) . ": " .
109 $this->msg( "internalerror-fatal-exception",
110 "Fatal exception of type $1",
113 MWExceptionHandler
::getURL( $this )
116 "<!-- Set \$wgShowExceptionDetails = true; " .
117 "at the bottom of LocalSettings.php to show detailed " .
118 "debugging information. -->";
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.
129 public function getText() {
130 global $wgShowExceptionDetails;
132 if ( $wgShowExceptionDetails ) {
133 return MWExceptionHandler
::getLogMessage( $this ) .
134 "\nBacktrace:\n" . MWExceptionHandler
::getRedactedTraceAsString( $this ) . "\n";
136 return "Set \$wgShowExceptionDetails = true; " .
137 "in LocalSettings.php to show detailed debugging information.\n";
142 * Return the title of the page when reporting this error in a HTTP response.
146 public function getPageTitle() {
147 return $this->msg( 'internalerror', 'Internal error' );
151 * Output the exception report using HTML.
153 public function reportHTML() {
154 global $wgOut, $wgSitename;
155 if ( $this->useOutputPage() ) {
156 $wgOut->prepareErrorPage( $this->getPageTitle() );
158 $wgOut->addHTML( $this->getHTML() );
162 self
::header( 'Content-Type: text/html; charset=utf-8' );
163 echo "<!DOCTYPE html>\n" .
165 // Mimick OutputPage::setPageTitle behaviour
167 htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
169 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
172 echo $this->getHTML();
174 echo "</body></html>\n";
179 * Output a report about the exception and takes care of formatting.
180 * It will be either HTML or plain text based on isCommandLine().
182 public function report() {
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 );
198 self
::statusHeader( 500 );
199 self
::header( "Content-Type: $wgMimeType; charset=utf-8" );
206 * Check whether we are in command line mode or not to report the exception
207 * in the correct format.
211 public static function isCommandLine() {
212 return !empty( $GLOBALS['wgCommandLineMode'] );
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
220 private static function header( $header ) {
221 if ( !headers_sent() ) {
225 private static function statusHeader( $code ) {
226 if ( !headers_sent() ) {
227 HttpStatus
::header( $code );