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 /** Get a message from i18n */
20 function msg( $key, $fallback /*[, params...] */ ) {
21 $args = array_slice( func_get_args(), 2 );
22 if ( $this->useMessageCache() ) {
23 return wfMsgReal( $key, $args );
25 return wfMsgReplaceArgs( $fallback, $args );
29 /* If wgShowExceptionDetails, return a HTML message with a backtrace to the error. */
31 global $wgShowExceptionDetails;
32 if( $wgShowExceptionDetails ) {
33 return '<p>' . htmlspecialchars( $this->getMessage() ) .
34 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
37 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
38 "at the bottom of LocalSettings.php to show detailed " .
39 "debugging information.</p>";
43 /* If wgShowExceptionDetails, return a text message with a backtrace to the error */
45 global $wgShowExceptionDetails;
46 if( $wgShowExceptionDetails ) {
47 return $this->getMessage() .
48 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
50 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
51 "in LocalSettings.php to show detailed debugging information.</p>";
55 /* Return titles of this error page */
56 function getPageTitle() {
57 if ( $this->useMessageCache() ) {
58 return wfMsg( 'internalerror' );
61 return "$wgSitename error";
65 /** Return the requested URL and point to file and line number from which the
68 function getLogMessage() {
70 $file = $this->getFile();
71 $line = $this->getLine();
72 $message = $this->getMessage();
73 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
76 /** Output the exception report using HTML */
77 function reportHTML() {
79 if ( $this->useOutputPage() ) {
80 $wgOut->setPageTitle( $this->getPageTitle() );
81 $wgOut->setRobotpolicy( "noindex,nofollow" );
82 $wgOut->setArticleRelated( false );
83 $wgOut->enableClientCache( false );
84 $wgOut->redirect( '' );
86 $wgOut->addHTML( $this->getHTML() );
89 echo $this->htmlHeader();
90 echo $this->getHTML();
91 echo $this->htmlFooter();
95 /* Output a report about the exception and takes care of formatting.
96 * It will be either HTML or plain text based on $wgCommandLineMode.
99 global $wgCommandLineMode;
100 if ( $wgCommandLineMode ) {
101 fwrite( STDERR
, $this->getText() );
103 $log = $this->getLogMessage();
105 wfDebugLog( 'exception', $log );
111 function htmlHeader() {
112 global $wgLogo, $wgSitename, $wgOutputEncoding;
114 if ( !headers_sent() ) {
115 header( 'HTTP/1.0 500 Internal Server Error' );
116 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
117 /* Don't cache error pages! They cause no end of trouble... */
118 header( 'Cache-control: none' );
119 header( 'Pragma: nocache' );
121 $title = $this->getPageTitle();
124 <title>$title</title>
127 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
131 function htmlFooter() {
132 echo "</body></html>";
138 * Exception class which takes an HTML error message, and does not
139 * produce a backtrace. Replacement for OutputPage::fatalError().
140 * @addtogroup Exception
142 class FatalError
extends MWException
{
144 return $this->getMessage();
148 return $this->getMessage();
153 * @addtogroup Exception
155 class ErrorPageError
extends MWException
{
159 * Note: these arguments are keys into wfMsg(), not text!
161 function __construct( $title, $msg ) {
162 $this->title
= $title;
164 parent
::__construct( wfMsg( $msg ) );
169 $wgOut->showErrorPage( $this->title
, $this->msg
);
175 * Install an exception handler for MediaWiki exception types.
177 function wfInstallExceptionHandler() {
178 set_exception_handler( 'wfExceptionHandler' );
182 * Report an exception to the user
184 function wfReportException( Exception
$e ) {
185 if ( $e instanceof MWException
) {
188 } catch ( Exception
$e2 ) {
189 // Exception occurred from within exception handler
190 // Show a simpler error message for the original exception,
191 // don't try to invoke report()
192 $message = "MediaWiki internal error.\n\n" .
193 "Original exception: " . $e->__toString() .
194 "\n\nException caught inside exception handler: " .
195 $e2->__toString() . "\n";
197 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
198 fwrite( STDERR
, $message );
200 echo nl2br( htmlspecialchars( $message ) ). "\n";
204 echo $e->__toString();
209 * Exception handler which simulates the appropriate catch() handling:
213 * } catch ( MWException $e ) {
215 * } catch ( Exception $e ) {
216 * echo $e->__toString();
219 function wfExceptionHandler( $e ) {
220 global $wgFullyInitialised;
221 wfReportException( $e );
223 // Final cleanup, similar to wfErrorExit()
224 if ( $wgFullyInitialised ) {
226 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
227 } catch ( Exception
$e ) {}
230 // Exit value should be nonzero for the benefit of shell jobs