3 * @defgroup Exception Exception
10 class MWException
extends Exception
{
12 * Should the exception use $wgOut to output the error ?
15 function useOutputPage() {
16 return $this->useMessageCache() &&
17 !empty( $GLOBALS['wgFullyInitialised'] ) &&
18 ( !empty( $GLOBALS['wgArticle'] ) ||
( !empty( $GLOBALS['wgOut'] ) && !$GLOBALS['wgOut']->isArticle() ) ) &&
19 !empty( $GLOBALS['wgTitle'] );
23 * Can the extension use wfMsg() to get i18n messages ?
26 function useMessageCache() {
28 foreach ( $this->getTrace() as $frame ) {
29 if ( isset( $frame['class'] ) &&
30 $frame['class'] == 'LocalisationCache' ) {
34 return is_object( $wgLang );
38 * Run hook to allow extensions to modify the text of the exception
40 * @param String $name class name of the exception
41 * @param Array $args arguments to pass to the callback functions
42 * @return mixed string to output or null if any hook has been called
44 function runHooks( $name, $args = array() ) {
45 global $wgExceptionHooks;
46 if( !isset( $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks ) )
47 return; // Just silently ignore
48 if( !array_key_exists( $name, $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks[ $name ] ) )
50 $hooks = $wgExceptionHooks[ $name ];
51 $callargs = array_merge( array( $this ), $args );
53 foreach( $hooks as $hook ) {
54 if( is_string( $hook ) ||
( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { //'function' or array( 'class', hook' )
55 $result = call_user_func_array( $hook, $callargs );
59 if( is_string( $result ) )
65 * Get a message from i18n
67 * @param String $key message name
68 * @param String $fallback default message if the message cache can't be
69 * called by the exception
70 * The function also has other parameters that are arguments for the message
71 * @return String message with arguments replaced
73 function msg( $key, $fallback /*[, params...] */ ) {
74 $args = array_slice( func_get_args(), 2 );
75 if ( $this->useMessageCache() ) {
76 return wfMsgReal( $key, $args );
78 return wfMsgReplaceArgs( $fallback, $args );
83 * If $wgShowExceptionDetails is true, return a HTML message with a
84 * backtrace to the error, otherwise show a message to ask to set it to true
85 * to show that information.
87 * @return String html to output
90 global $wgShowExceptionDetails;
91 if( $wgShowExceptionDetails ) {
92 return '<p>' . nl2br( htmlspecialchars( $this->getMessage() ) ) .
93 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
96 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
97 "at the bottom of LocalSettings.php to show detailed " .
98 "debugging information.</p>";
103 * If $wgShowExceptionDetails is true, return a text message with a
104 * backtrace to the error.
107 global $wgShowExceptionDetails;
108 if( $wgShowExceptionDetails ) {
109 return $this->getMessage() .
110 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
112 return "Set \$wgShowExceptionDetails = true; " .
113 "in LocalSettings.php to show detailed debugging information.\n";
117 /* Return titles of this error page */
118 function getPageTitle() {
119 if ( $this->useMessageCache() ) {
120 return wfMsg( 'internalerror' );
123 return "$wgSitename error";
128 * Return the requested URL and point to file and line number from which the
133 function getLogMessage() {
135 $file = $this->getFile();
136 $line = $this->getLine();
137 $message = $this->getMessage();
138 if ( isset( $wgRequest ) ) {
139 $url = $wgRequest->getRequestURL();
147 return "$url Exception from line $line of $file: $message";
150 /** Output the exception report using HTML */
151 function reportHTML() {
153 if ( $this->useOutputPage() ) {
154 $wgOut->setPageTitle( $this->getPageTitle() );
155 $wgOut->setRobotPolicy( "noindex,nofollow" );
156 $wgOut->setArticleRelated( false );
157 $wgOut->enableClientCache( false );
158 $wgOut->redirect( '' );
160 if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
161 $wgOut->addHTML( $hookResult );
163 $wgOut->addHTML( $this->getHTML() );
167 if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
170 if ( defined( 'MEDIAWIKI_INSTALL' ) ||
$this->htmlBodyOnly() ) {
171 echo $this->getHTML();
173 echo $this->htmlHeader();
174 echo $this->getHTML();
175 echo $this->htmlFooter();
181 * Output a report about the exception and takes care of formatting.
182 * It will be either HTML or plain text based on isCommandLine().
185 $log = $this->getLogMessage();
187 wfDebugLog( 'exception', $log );
189 if ( self
::isCommandLine() ) {
190 wfPrintError( $this->getText() );
197 * Send headers and output the beginning of the html page if not using
198 * $wgOut to output the exception.
200 function htmlHeader() {
201 global $wgLogo, $wgSitename, $wgOutputEncoding;
203 if ( !headers_sent() ) {
204 header( 'HTTP/1.0 500 Internal Server Error' );
205 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
206 /* Don't cache error pages! They cause no end of trouble... */
207 header( 'Cache-control: none' );
208 header( 'Pragma: nocache' );
210 $title = $this->getPageTitle();
213 <title>$title</title>
216 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''/>$title</h1>
221 * print the end of the html page if not using $wgOut.
223 function htmlFooter() {
224 return "</body></html>";
228 * headers handled by subclass?
230 function htmlBodyOnly() {
234 static function isCommandLine() {
235 return !empty( $GLOBALS['wgCommandLineMode'] ) && !defined( 'MEDIAWIKI_INSTALL' );
240 * Exception class which takes an HTML error message, and does not
241 * produce a backtrace. Replacement for OutputPage::fatalError().
244 class FatalError
extends MWException
{
246 return $this->getMessage();
250 return $this->getMessage();
257 class ErrorPageError
extends MWException
{
261 * Note: these arguments are keys into wfMsg(), not text!
263 function __construct( $title, $msg ) {
264 $this->title
= $title;
266 parent
::__construct( wfMsg( $msg ) );
271 $wgOut->showErrorPage( $this->title
, $this->msg
);
277 * Install an exception handler for MediaWiki exception types.
279 function wfInstallExceptionHandler() {
280 set_exception_handler( 'wfExceptionHandler' );
284 * Report an exception to the user
286 function wfReportException( Exception
$e ) {
287 $cmdLine = MWException
::isCommandLine();
288 if ( $e instanceof MWException
) {
291 } catch ( Exception
$e2 ) {
292 // Exception occurred from within exception handler
293 // Show a simpler error message for the original exception,
294 // don't try to invoke report()
295 $message = "MediaWiki internal error.\n\n";
296 if ( $GLOBALS['wgShowExceptionDetails'] )
297 $message .= "Original exception: " . $e->__toString();
298 $message .= "\n\nException caught inside exception handler";
299 if ( $GLOBALS['wgShowExceptionDetails'] )
300 $message .= ": " . $e2->__toString();
303 wfPrintError( $message );
305 echo nl2br( htmlspecialchars( $message ) ). "\n";
309 $message = "Unexpected non-MediaWiki exception encountered, of type \"" . get_class( $e ) . "\"\n" .
310 $e->__toString() . "\n";
311 if ( $GLOBALS['wgShowExceptionDetails'] ) {
312 $message .= "\n" . $e->getTraceAsString() ."\n";
315 wfPrintError( $message );
317 echo nl2br( htmlspecialchars( $message ) ). "\n";
323 * Print a message, if possible to STDERR.
324 * Use this in command line mode only (see isCommandLine)
326 function wfPrintError( $message ) {
327 #NOTE: STDERR may not be available, especially if php-cgi is used from the command line (bug #15602).
328 # Try to produce meaningful output anyway. Using echo may corrupt output to STDOUT though.
329 if ( defined( 'STDERR' ) ) {
330 fwrite( STDERR
, $message );
337 * Exception handler which simulates the appropriate catch() handling:
341 * } catch ( MWException $e ) {
343 * } catch ( Exception $e ) {
344 * echo $e->__toString();
347 function wfExceptionHandler( $e ) {
348 global $wgFullyInitialised;
349 wfReportException( $e );
351 // Final cleanup, similar to wfErrorExit()
352 if ( $wgFullyInitialised ) {
354 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
355 } catch ( Exception
$e ) {}
358 // Exit value should be nonzero for the benefit of shell jobs