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 /** Print the exception report using text */
96 function reportText() {
97 echo $this->getText();
100 /* Output a report about the exception and takes care of formatting.
101 * It will be either HTML or plain text based on $wgCommandLineMode.
104 global $wgCommandLineMode;
105 if ( $wgCommandLineMode ) {
108 $log = $this->getLogMessage();
110 wfDebugLog( 'exception', $log );
116 function htmlHeader() {
117 global $wgLogo, $wgSitename, $wgOutputEncoding;
119 if ( !headers_sent() ) {
120 header( 'HTTP/1.0 500 Internal Server Error' );
121 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
122 /* Don't cache error pages! They cause no end of trouble... */
123 header( 'Cache-control: none' );
124 header( 'Pragma: nocache' );
126 $title = $this->getPageTitle();
129 <title>$title</title>
132 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
136 function htmlFooter() {
137 echo "</body></html>";
143 * Exception class which takes an HTML error message, and does not
144 * produce a backtrace. Replacement for OutputPage::fatalError().
145 * @addtogroup Exception
147 class FatalError
extends MWException
{
149 return $this->getMessage();
153 return $this->getMessage();
158 * @addtogroup Exception
160 class ErrorPageError
extends MWException
{
164 * Note: these arguments are keys into wfMsg(), not text!
166 function __construct( $title, $msg ) {
167 $this->title
= $title;
169 parent
::__construct( wfMsg( $msg ) );
174 $wgOut->showErrorPage( $this->title
, $this->msg
);
180 * Install an exception handler for MediaWiki exception types.
182 function wfInstallExceptionHandler() {
183 set_exception_handler( 'wfExceptionHandler' );
187 * Report an exception to the user
189 function wfReportException( Exception
$e ) {
190 if ( $e instanceof MWException
) {
193 } catch ( Exception
$e2 ) {
194 // Exception occurred from within exception handler
195 // Show a simpler error message for the original exception,
196 // don't try to invoke report()
197 $message = "MediaWiki internal error.\n\n" .
198 "Original exception: " . $e->__toString() .
199 "\n\nException caught inside exception handler: " .
200 $e2->__toString() . "\n";
202 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
205 echo nl2br( htmlspecialchars( $message ) ). "\n";
209 echo $e->__toString();
214 * Exception handler which simulates the appropriate catch() handling:
218 * } catch ( MWException $e ) {
220 * } catch ( Exception $e ) {
221 * echo $e->__toString();
224 function wfExceptionHandler( $e ) {
225 global $wgFullyInitialised;
226 wfReportException( $e );
228 // Final cleanup, similar to wfErrorExit()
229 if ( $wgFullyInitialised ) {
231 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
232 } catch ( Exception
$e ) {}
235 // Exit value should be nonzero for the benefit of shell jobs