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 ( $frame['class'] == 'LocalisationCache' ) {
33 return is_object( $wgLang );
37 * Run hook to allow extensions to modify the text of the exception
39 * @param String $name class name of the exception
40 * @param Array $args arguments to pass to the callback functions
41 * @return mixed string to output or null if any hook has been called
43 function runHooks( $name, $args = array() ) {
44 global $wgExceptionHooks;
45 if( !isset( $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks ) )
46 return; // Just silently ignore
47 if( !array_key_exists( $name, $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks[ $name ] ) )
49 $hooks = $wgExceptionHooks[ $name ];
50 $callargs = array_merge( array( $this ), $args );
52 foreach( $hooks as $hook ) {
53 if( is_string( $hook ) ||
( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { //'function' or array( 'class', hook' )
54 $result = call_user_func_array( $hook, $callargs );
58 if( is_string( $result ) )
64 * Get a message from i18n
66 * @param String $key message name
67 * @param String $fallback default message if the message cache can't be
68 * called by the exception
69 * The function also has other parameters that are arguments for the message
70 * @return String message with arguments replaced
72 function msg( $key, $fallback /*[, params...] */ ) {
73 $args = array_slice( func_get_args(), 2 );
74 if ( $this->useMessageCache() ) {
75 return wfMsgReal( $key, $args );
77 return wfMsgReplaceArgs( $fallback, $args );
82 * If $wgShowExceptionDetails is true, return a HTML message with a
83 * backtrace to the error, otherwise show a message to ask to set it to true
84 * to show that information.
86 * @return String html to output
89 global $wgShowExceptionDetails;
90 if( $wgShowExceptionDetails ) {
91 return '<p>' . nl2br( htmlspecialchars( $this->getMessage() ) ) .
92 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
95 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
96 "at the bottom of LocalSettings.php to show detailed " .
97 "debugging information.</p>";
102 * If $wgShowExceptionDetails is true, return a text message with a
103 * backtrace to the error.
106 global $wgShowExceptionDetails;
107 if( $wgShowExceptionDetails ) {
108 return $this->getMessage() .
109 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
111 return "Set \$wgShowExceptionDetails = true; " .
112 "in LocalSettings.php to show detailed debugging information.\n";
116 /* Return titles of this error page */
117 function getPageTitle() {
118 if ( $this->useMessageCache() ) {
119 return wfMsg( 'internalerror' );
122 return "$wgSitename error";
127 * Return the requested URL and point to file and line number from which the
132 function getLogMessage() {
134 $file = $this->getFile();
135 $line = $this->getLine();
136 $message = $this->getMessage();
137 if ( isset( $wgRequest ) ) {
138 $url = $wgRequest->getRequestURL();
146 return "$url Exception from line $line of $file: $message";
149 /** Output the exception report using HTML */
150 function reportHTML() {
152 if ( $this->useOutputPage() ) {
153 $wgOut->setPageTitle( $this->getPageTitle() );
154 $wgOut->setRobotPolicy( "noindex,nofollow" );
155 $wgOut->setArticleRelated( false );
156 $wgOut->enableClientCache( false );
157 $wgOut->redirect( '' );
159 if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
160 $wgOut->addHTML( $hookResult );
162 $wgOut->addHTML( $this->getHTML() );
166 if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
169 if ( defined( 'MEDIAWIKI_INSTALL' ) ||
$this->htmlBodyOnly() ) {
170 echo $this->getHTML();
172 echo $this->htmlHeader();
173 echo $this->getHTML();
174 echo $this->htmlFooter();
180 * Output a report about the exception and takes care of formatting.
181 * It will be either HTML or plain text based on isCommandLine().
184 $log = $this->getLogMessage();
186 wfDebugLog( 'exception', $log );
188 if ( self
::isCommandLine() ) {
189 wfPrintError( $this->getText() );
196 * Send headers and output the beginning of the html page if not using
197 * $wgOut to output the exception.
199 function htmlHeader() {
200 global $wgLogo, $wgSitename, $wgOutputEncoding;
202 if ( !headers_sent() ) {
203 header( 'HTTP/1.0 500 Internal Server Error' );
204 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
205 /* Don't cache error pages! They cause no end of trouble... */
206 header( 'Cache-control: none' );
207 header( 'Pragma: nocache' );
209 $title = $this->getPageTitle();
212 <title>$title</title>
215 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''/>$title</h1>
220 * print the end of the html page if not using $wgOut.
222 function htmlFooter() {
223 return "</body></html>";
227 * headers handled by subclass?
229 function htmlBodyOnly() {
233 static function isCommandLine() {
234 return !empty( $GLOBALS['wgCommandLineMode'] ) && !defined( 'MEDIAWIKI_INSTALL' );
239 * Exception class which takes an HTML error message, and does not
240 * produce a backtrace. Replacement for OutputPage::fatalError().
243 class FatalError
extends MWException
{
245 return $this->getMessage();
249 return $this->getMessage();
256 class ErrorPageError
extends MWException
{
260 * Note: these arguments are keys into wfMsg(), not text!
262 function __construct( $title, $msg ) {
263 $this->title
= $title;
265 parent
::__construct( wfMsg( $msg ) );
270 $wgOut->showErrorPage( $this->title
, $this->msg
);
276 * Install an exception handler for MediaWiki exception types.
278 function wfInstallExceptionHandler() {
279 set_exception_handler( 'wfExceptionHandler' );
283 * Report an exception to the user
285 function wfReportException( Exception
$e ) {
286 $cmdLine = MWException
::isCommandLine();
287 if ( $e instanceof MWException
) {
290 } catch ( Exception
$e2 ) {
291 // Exception occurred from within exception handler
292 // Show a simpler error message for the original exception,
293 // don't try to invoke report()
294 $message = "MediaWiki internal error.\n\n";
295 if ( $GLOBALS['wgShowExceptionDetails'] )
296 $message .= "Original exception: " . $e->__toString();
297 $message .= "\n\nException caught inside exception handler";
298 if ( $GLOBALS['wgShowExceptionDetails'] )
299 $message .= ": " . $e2->__toString();
302 wfPrintError( $message );
304 echo nl2br( htmlspecialchars( $message ) ). "\n";
308 $message = "Unexpected non-MediaWiki exception encountered, of type \"" . get_class( $e ) . "\"\n" .
309 $e->__toString() . "\n";
310 if ( $GLOBALS['wgShowExceptionDetails'] ) {
311 $message .= "\n" . $e->getTraceAsString() ."\n";
314 wfPrintError( $message );
316 echo nl2br( htmlspecialchars( $message ) ). "\n";
322 * Print a message, if possible to STDERR.
323 * Use this in command line mode only (see isCommandLine)
325 function wfPrintError( $message ) {
326 #NOTE: STDERR may not be available, especially if php-cgi is used from the command line (bug #15602).
327 # Try to produce meaningful output anyway. Using echo may corrupt output to STDOUT though.
328 if ( defined( 'STDERR' ) ) {
329 fwrite( STDERR
, $message );
336 * Exception handler which simulates the appropriate catch() handling:
340 * } catch ( MWException $e ) {
342 * } catch ( Exception $e ) {
343 * echo $e->__toString();
346 function wfExceptionHandler( $e ) {
347 global $wgFullyInitialised;
348 wfReportException( $e );
350 // Final cleanup, similar to wfErrorExit()
351 if ( $wgFullyInitialised ) {
353 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
354 } catch ( Exception
$e ) {}
357 // Exit value should be nonzero for the benefit of shell jobs