5 * @addtogroup Exception
7 class MWException
extends Exception
9 function useOutputPage() {
10 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
11 !empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
14 function useMessageCache() {
16 return is_object( $wgLang );
19 function runHooks( $name, $args = array() ) {
20 global $wgExceptionHooks;
21 if( !isset( $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks ) )
22 return; // Just silently ignore
23 if( !array_key_exists( $name, $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks[ $name ] ) )
25 $hooks = $wgExceptionHooks[ $name ];
26 $callargs = array_merge( array( $this ), $args );
28 foreach( $hooks as $hook ) {
29 if( is_string( $hook ) ||
( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { //'function' or array( 'class', hook' )
30 $result = call_user_func_array( $hook, $callargs );
34 if( is_string( $result ) )
39 /** Get a message from i18n */
40 function msg( $key, $fallback /*[, params...] */ ) {
41 $args = array_slice( func_get_args(), 2 );
42 if ( $this->useMessageCache() ) {
43 return wfMsgReal( $key, $args );
45 return wfMsgReplaceArgs( $fallback, $args );
49 /* If wgShowExceptionDetails, return a HTML message with a backtrace to the error. */
51 global $wgShowExceptionDetails;
52 if( $wgShowExceptionDetails ) {
53 return '<p>' . htmlspecialchars( $this->getMessage() ) .
54 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
57 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
58 "at the bottom of LocalSettings.php to show detailed " .
59 "debugging information.</p>";
63 /* If wgShowExceptionDetails, return a text message with a backtrace to the error */
65 global $wgShowExceptionDetails;
66 if( $wgShowExceptionDetails ) {
67 return $this->getMessage() .
68 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
70 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
71 "in LocalSettings.php to show detailed debugging information.</p>";
75 /* Return titles of this error page */
76 function getPageTitle() {
77 if ( $this->useMessageCache() ) {
78 return wfMsg( 'internalerror' );
81 return "$wgSitename error";
85 /** Return the requested URL and point to file and line number from which the
88 function getLogMessage() {
90 $file = $this->getFile();
91 $line = $this->getLine();
92 $message = $this->getMessage();
93 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
96 /** Output the exception report using HTML */
97 function reportHTML() {
99 if ( $this->useOutputPage() ) {
100 $wgOut->setPageTitle( $this->getPageTitle() );
101 $wgOut->setRobotpolicy( "noindex,nofollow" );
102 $wgOut->setArticleRelated( false );
103 $wgOut->enableClientCache( false );
104 $wgOut->redirect( '' );
106 if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
107 $wgOut->addHTML( $hookResult );
109 $wgOut->addHTML( $this->getHTML() );
113 if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
116 echo $this->htmlHeader();
117 echo $this->getHTML();
118 echo $this->htmlFooter();
122 /* Output a report about the exception and takes care of formatting.
123 * It will be either HTML or plain text based on $wgCommandLineMode.
126 global $wgCommandLineMode;
127 if ( $wgCommandLineMode ) {
128 fwrite( STDERR
, $this->getText() );
130 $log = $this->getLogMessage();
132 wfDebugLog( 'exception', $log );
138 function htmlHeader() {
139 global $wgLogo, $wgSitename, $wgOutputEncoding;
141 if ( !headers_sent() ) {
142 header( 'HTTP/1.0 500 Internal Server Error' );
143 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
144 /* Don't cache error pages! They cause no end of trouble... */
145 header( 'Cache-control: none' );
146 header( 'Pragma: nocache' );
148 $title = $this->getPageTitle();
151 <title>$title</title>
154 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
158 function htmlFooter() {
159 echo "</body></html>";
164 * Exception class which takes an HTML error message, and does not
165 * produce a backtrace. Replacement for OutputPage::fatalError().
166 * @addtogroup Exception
168 class FatalError
extends MWException
{
170 return $this->getMessage();
174 return $this->getMessage();
179 * @addtogroup Exception
181 class ErrorPageError
extends MWException
{
185 * Note: these arguments are keys into wfMsg(), not text!
187 function __construct( $title, $msg ) {
188 $this->title
= $title;
190 parent
::__construct( wfMsg( $msg ) );
195 $wgOut->showErrorPage( $this->title
, $this->msg
);
201 * Install an exception handler for MediaWiki exception types.
203 function wfInstallExceptionHandler() {
204 set_exception_handler( 'wfExceptionHandler' );
208 * Report an exception to the user
210 function wfReportException( Exception
$e ) {
211 if ( $e instanceof MWException
) {
214 } catch ( Exception
$e2 ) {
215 // Exception occurred from within exception handler
216 // Show a simpler error message for the original exception,
217 // don't try to invoke report()
218 $message = "MediaWiki internal error.\n\n" .
219 "Original exception: " . $e->__toString() .
220 "\n\nException caught inside exception handler: " .
221 $e2->__toString() . "\n";
223 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
224 fwrite( STDERR
, $message );
226 echo nl2br( htmlspecialchars( $message ) ). "\n";
230 echo $e->__toString();
235 * Exception handler which simulates the appropriate catch() handling:
239 * } catch ( MWException $e ) {
241 * } catch ( Exception $e ) {
242 * echo $e->__toString();
245 function wfExceptionHandler( $e ) {
246 global $wgFullyInitialised;
247 wfReportException( $e );
249 // Final cleanup, similar to wfErrorExit()
250 if ( $wgFullyInitialised ) {
252 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
253 } catch ( Exception
$e ) {}
256 // Exit value should be nonzero for the benefit of shell jobs