5 * @addtogroup Exception
7 class MWException
extends Exception
{
10 * Should the exception use $wgOut to output the error ?
13 function useOutputPage() {
14 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
15 ( !empty( $GLOBALS['wgArticle'] ) ||
( !empty( $GLOBALS['wgOut'] ) && !$GLOBALS['wgOut']->isArticle() ) ) &&
16 !empty( $GLOBALS['wgTitle'] );
20 * Can the extension use wfMsg() to get i18n messages ?
23 function useMessageCache() {
25 return is_object( $wgLang );
29 * Run hook to allow extensions to modify the text of the exception
31 * @param String $name class name of the exception
32 * @param Array $args arguments to pass to the callback functions
33 * @return mixed string to output or null if any hook has been called
35 function runHooks( $name, $args = array() ) {
36 global $wgExceptionHooks;
37 if( !isset( $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks ) )
38 return; // Just silently ignore
39 if( !array_key_exists( $name, $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks[ $name ] ) )
41 $hooks = $wgExceptionHooks[ $name ];
42 $callargs = array_merge( array( $this ), $args );
44 foreach( $hooks as $hook ) {
45 if( is_string( $hook ) ||
( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { //'function' or array( 'class', hook' )
46 $result = call_user_func_array( $hook, $callargs );
50 if( is_string( $result ) )
56 * Get a message from i18n
58 * @param String $key message name
59 * @param String $fallback default message if the message cache can't be
60 * called by the exception
61 * The function also has other parameters that are arguments for the message
62 * @return String message with arguments replaced
64 function msg( $key, $fallback /*[, params...] */ ) {
65 $args = array_slice( func_get_args(), 2 );
66 if ( $this->useMessageCache() ) {
67 return wfMsgReal( $key, $args );
69 return wfMsgReplaceArgs( $fallback, $args );
74 * If $wgShowExceptionDetails is true, return a HTML message with a
75 * backtrace to the error, otherwise show a message to ask to set it to true
76 * to show that information.
78 * @return String html to output
81 global $wgShowExceptionDetails;
82 if( $wgShowExceptionDetails ) {
83 return '<p>' . htmlspecialchars( $this->getMessage() ) .
84 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
87 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
88 "at the bottom of LocalSettings.php to show detailed " .
89 "debugging information.</p>";
94 * If $wgShowExceptionDetails is true, return a text message with a
95 * backtrace to the error.
98 global $wgShowExceptionDetails;
99 if( $wgShowExceptionDetails ) {
100 return $this->getMessage() .
101 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
103 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
104 "in LocalSettings.php to show detailed debugging information.</p>";
108 /* Return titles of this error page */
109 function getPageTitle() {
110 if ( $this->useMessageCache() ) {
111 return wfMsg( 'internalerror' );
114 return "$wgSitename error";
119 * Return the requested URL and point to file and line number from which the
124 function getLogMessage() {
126 $file = $this->getFile();
127 $line = $this->getLine();
128 $message = $this->getMessage();
129 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
132 /** Output the exception report using HTML */
133 function reportHTML() {
135 if ( $this->useOutputPage() ) {
136 $wgOut->setPageTitle( $this->getPageTitle() );
137 $wgOut->setRobotpolicy( "noindex,nofollow" );
138 $wgOut->setArticleRelated( false );
139 $wgOut->enableClientCache( false );
140 $wgOut->redirect( '' );
142 if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
143 $wgOut->addHTML( $hookResult );
145 $wgOut->addHTML( $this->getHTML() );
149 if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
152 echo $this->htmlHeader();
153 echo $this->getHTML();
154 echo $this->htmlFooter();
159 * Output a report about the exception and takes care of formatting.
160 * It will be either HTML or plain text based on $wgCommandLineMode.
163 global $wgCommandLineMode;
164 if ( $wgCommandLineMode ) {
165 fwrite( STDERR
, $this->getText() );
167 $log = $this->getLogMessage();
169 wfDebugLog( 'exception', $log );
176 * Send headers and output the beginning of the html page if not using
177 * $wgOut to output the exception.
179 function htmlHeader() {
180 global $wgLogo, $wgSitename, $wgOutputEncoding;
182 if ( !headers_sent() ) {
183 header( 'HTTP/1.0 500 Internal Server Error' );
184 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
185 /* Don't cache error pages! They cause no end of trouble... */
186 header( 'Cache-control: none' );
187 header( 'Pragma: nocache' );
189 $title = $this->getPageTitle();
192 <title>$title</title>
195 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
200 * print the end of the html page if not using $wgOut.
202 function htmlFooter() {
203 echo "</body></html>";
208 * Exception class which takes an HTML error message, and does not
209 * produce a backtrace. Replacement for OutputPage::fatalError().
210 * @addtogroup Exception
212 class FatalError
extends MWException
{
214 return $this->getMessage();
218 return $this->getMessage();
223 * @addtogroup Exception
225 class ErrorPageError
extends MWException
{
229 * Note: these arguments are keys into wfMsg(), not text!
231 function __construct( $title, $msg ) {
232 $this->title
= $title;
234 parent
::__construct( wfMsg( $msg ) );
239 $wgOut->showErrorPage( $this->title
, $this->msg
);
245 * Install an exception handler for MediaWiki exception types.
247 function wfInstallExceptionHandler() {
248 set_exception_handler( 'wfExceptionHandler' );
252 * Report an exception to the user
254 function wfReportException( Exception
$e ) {
255 if ( $e instanceof MWException
) {
258 } catch ( Exception
$e2 ) {
259 // Exception occurred from within exception handler
260 // Show a simpler error message for the original exception,
261 // don't try to invoke report()
262 $message = "MediaWiki internal error.\n\n" .
263 "Original exception: " . $e->__toString() .
264 "\n\nException caught inside exception handler: " .
265 $e2->__toString() . "\n";
267 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
268 fwrite( STDERR
, $message );
270 echo nl2br( htmlspecialchars( $message ) ). "\n";
274 echo $e->__toString();
279 * Exception handler which simulates the appropriate catch() handling:
283 * } catch ( MWException $e ) {
285 * } catch ( Exception $e ) {
286 * echo $e->__toString();
289 function wfExceptionHandler( $e ) {
290 global $wgFullyInitialised;
291 wfReportException( $e );
293 // Final cleanup, similar to wfErrorExit()
294 if ( $wgFullyInitialised ) {
296 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
297 } catch ( Exception
$e ) {}
300 // Exit value should be nonzero for the benefit of shell jobs