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' ) {
63 return $wgLang instanceof Language
;
67 * Run hook to allow extensions to modify the text of the exception
69 * @param string $name class name of the exception
70 * @param array $args arguments to pass to the callback functions
71 * @return string|null string to output or null if any hook has been called
73 public function runHooks( $name, $args = array() ) {
74 global $wgExceptionHooks;
76 if ( !isset( $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks ) ) {
77 return null; // Just silently ignore
80 if ( !array_key_exists( $name, $wgExceptionHooks ) ||
81 !is_array( $wgExceptionHooks[$name] )
86 $hooks = $wgExceptionHooks[$name];
87 $callargs = array_merge( array( $this ), $args );
89 foreach ( $hooks as $hook ) {
92 ( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) )
94 // 'function' or array( 'class', hook' )
95 $result = call_user_func_array( $hook, $callargs );
100 if ( is_string( $result ) ) {
108 * Get a message from i18n
110 * @param string $key message name
111 * @param string $fallback default message if the message cache can't be
112 * called by the exception
113 * The function also has other parameters that are arguments for the message
114 * @return string message with arguments replaced
116 public function msg( $key, $fallback /*[, params...] */ ) {
117 $args = array_slice( func_get_args(), 2 );
119 if ( $this->useMessageCache() ) {
120 return wfMessage( $key, $args )->text();
122 return wfMsgReplaceArgs( $fallback, $args );
127 * If $wgShowExceptionDetails is true, return a HTML message with a
128 * backtrace to the error, otherwise show a message to ask to set it to true
129 * to show that information.
131 * @return string html to output
133 public function getHTML() {
134 global $wgShowExceptionDetails;
136 if ( $wgShowExceptionDetails ) {
137 return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler
::getLogMessage( $this ) ) ) .
138 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( MWExceptionHandler
::getRedactedTraceAsString( $this ) ) ) .
141 return "<div class=\"errorbox\">" .
142 '[' . MWExceptionHandler
::getLogId( $this ) . '] ' .
143 gmdate( 'Y-m-d H:i:s' ) .
144 ": Fatal exception of type " . get_class( $this ) . "</div>\n" .
145 "<!-- Set \$wgShowExceptionDetails = true; " .
146 "at the bottom of LocalSettings.php to show detailed " .
147 "debugging information. -->";
152 * Get the text to display when reporting the error on the command line.
153 * If $wgShowExceptionDetails is true, return a text message with a
154 * backtrace to the error.
158 public function getText() {
159 global $wgShowExceptionDetails;
161 if ( $wgShowExceptionDetails ) {
162 return MWExceptionHandler
::getLogMessage( $this ) .
163 "\nBacktrace:\n" . MWExceptionHandler
::getRedactedTraceAsString( $this ) . "\n";
165 return "Set \$wgShowExceptionDetails = true; " .
166 "in LocalSettings.php to show detailed debugging information.\n";
171 * Return the title of the page when reporting this error in a HTTP response.
175 public function getPageTitle() {
176 return $this->msg( 'internalerror', 'Internal error' );
180 * Get a the ID for this error.
183 * @deprecated since 1.22 Use MWExceptionHandler::getLogId instead.
186 public function getLogId() {
187 wfDeprecated( __METHOD__
, '1.22' );
188 return MWExceptionHandler
::getLogId( $this );
192 * Return the requested URL and point to file and line number from which the
196 * @deprecated since 1.22 Use MWExceptionHandler::getLogMessage instead.
199 public function getLogMessage() {
200 wfDeprecated( __METHOD__
, '1.22' );
201 return MWExceptionHandler
::getLogMessage( $this );
205 * Output the exception report using HTML.
207 public function reportHTML() {
208 global $wgOut, $wgSitename;
209 if ( $this->useOutputPage() ) {
210 $wgOut->prepareErrorPage( $this->getPageTitle() );
212 $hookResult = $this->runHooks( get_class( $this ) );
214 $wgOut->addHTML( $hookResult );
216 $wgOut->addHTML( $this->getHTML() );
221 header( 'Content-Type: text/html; charset=utf-8' );
222 echo "<!DOCTYPE html>\n" .
224 // Mimick OutputPage::setPageTitle behaviour
225 '<title>' . htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) . '</title>' .
226 '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
229 $hookResult = $this->runHooks( get_class( $this ) . 'Raw' );
233 echo $this->getHTML();
236 echo "</body></html>\n";
241 * Output a report about the exception and takes care of formatting.
242 * It will be either HTML or plain text based on isCommandLine().
244 public function report() {
247 MWExceptionHandler
::logException( $this );
249 if ( defined( 'MW_API' ) ) {
250 // Unhandled API exception, we can't be sure that format printer is alive
251 header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $this ) );
252 wfHttpError( 500, 'Internal Server Error', $this->getText() );
253 } elseif ( self
::isCommandLine() ) {
254 MWExceptionHandler
::printError( $this->getText() );
256 header( 'HTTP/1.1 500 MediaWiki exception' );
257 header( 'Status: 500 MediaWiki exception', true );
258 header( "Content-Type: $wgMimeType; charset=utf-8", true );
265 * Check whether we are in command line mode or not to report the exception
266 * in the correct format.
270 public static function isCommandLine() {
271 return !empty( $GLOBALS['wgCommandLineMode'] );