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 "in LocalSettings.php to show detailed debugging information.</p>";
42 /* If wgShowExceptionDetails, return a text message with a backtrace to the error */
44 global $wgShowExceptionDetails;
45 if( $wgShowExceptionDetails ) {
46 return $this->getMessage() .
47 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
49 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
50 "in LocalSettings.php to show detailed debugging information.</p>";
54 /* Return titles of this error page */
55 function getPageTitle() {
56 if ( $this->useMessageCache() ) {
57 return wfMsg( 'internalerror' );
60 return "$wgSitename error";
64 /** Return the requested URL and point to file and line number from which the
67 function getLogMessage() {
69 $file = $this->getFile();
70 $line = $this->getLine();
71 $message = $this->getMessage();
72 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
75 /** Output the exception report using HTML */
76 function reportHTML() {
78 if ( $this->useOutputPage() ) {
79 $wgOut->setPageTitle( $this->getPageTitle() );
80 $wgOut->setRobotpolicy( "noindex,nofollow" );
81 $wgOut->setArticleRelated( false );
82 $wgOut->enableClientCache( false );
83 $wgOut->redirect( '' );
85 $wgOut->addHTML( $this->getHTML() );
88 echo $this->htmlHeader();
89 echo $this->getHTML();
90 echo $this->htmlFooter();
94 /** Print the exception report using text */
95 function reportText() {
96 echo $this->getText();
99 /* Output a report about the exception and takes care of formatting.
100 * It will be either HTML or plain text based on $wgCommandLineMode.
103 global $wgCommandLineMode;
104 if ( $wgCommandLineMode ) {
107 $log = $this->getLogMessage();
109 wfDebugLog( 'exception', $log );
115 function htmlHeader() {
116 global $wgLogo, $wgSitename, $wgOutputEncoding;
118 if ( !headers_sent() ) {
119 header( 'HTTP/1.0 500 Internal Server Error' );
120 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
121 /* Don't cache error pages! They cause no end of trouble... */
122 header( 'Cache-control: none' );
123 header( 'Pragma: nocache' );
125 $title = $this->getPageTitle();
128 <title>$title</title>
131 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
135 function htmlFooter() {
136 echo "</body></html>";
142 * Exception class which takes an HTML error message, and does not
143 * produce a backtrace. Replacement for OutputPage::fatalError().
144 * @addtogroup Exception
146 class FatalError
extends MWException
{
148 return $this->getMessage();
152 return $this->getMessage();
157 * @addtogroup Exception
159 class ErrorPageError
extends MWException
{
163 * Note: these arguments are keys into wfMsg(), not text!
165 function __construct( $title, $msg ) {
166 $this->title
= $title;
168 parent
::__construct( wfMsg( $msg ) );
173 $wgOut->showErrorPage( $this->title
, $this->msg
);
179 * Install an exception handler for MediaWiki exception types.
181 function wfInstallExceptionHandler() {
182 set_exception_handler( 'wfExceptionHandler' );
186 * Report an exception to the user
188 function wfReportException( Exception
$e ) {
189 if ( $e instanceof MWException
) {
192 } catch ( Exception
$e2 ) {
193 // Exception occurred from within exception handler
194 // Show a simpler error message for the original exception,
195 // don't try to invoke report()
196 $message = "MediaWiki internal error.\n\n" .
197 "Original exception: " . $e->__toString() .
198 "\n\nException caught inside exception handler: " .
199 $e2->__toString() . "\n";
201 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
204 echo nl2br( htmlspecialchars( $message ) ). "\n";
208 echo $e->__toString();
213 * Exception handler which simulates the appropriate catch() handling:
217 * } catch ( MWException $e ) {
219 * } catch ( Exception $e ) {
220 * echo $e->__toString();
223 function wfExceptionHandler( $e ) {
224 global $wgFullyInitialised;
225 wfReportException( $e );
227 // Final cleanup, similar to wfErrorExit()
228 if ( $wgFullyInitialised ) {
230 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
231 } catch ( Exception
$e ) {}
234 // Exit value should be nonzero for the benefit of shell jobs