4 * @addtogroup Exception
6 class MWException
extends Exception
8 function useOutputPage() {
9 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
10 !empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
13 function useMessageCache() {
15 return is_object( $wgLang );
18 function msg( $key, $fallback /*[, params...] */ ) {
19 $args = array_slice( func_get_args(), 2 );
20 if ( $this->useMessageCache() ) {
21 return wfMsgReal( $key, $args );
23 return wfMsgReplaceArgs( $fallback, $args );
28 global $wgShowExceptionDetails;
29 if( $wgShowExceptionDetails ) {
30 return '<p>' . htmlspecialchars( $this->getMessage() ) .
31 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
34 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
35 "in LocalSettings.php to show detailed debugging information.</p>";
40 global $wgShowExceptionDetails;
41 if( $wgShowExceptionDetails ) {
42 return $this->getMessage() .
43 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
45 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
46 "in LocalSettings.php to show detailed debugging information.</p>";
50 function getPageTitle() {
51 if ( $this->useMessageCache() ) {
52 return wfMsg( 'internalerror' );
55 return "$wgSitename error";
59 function getLogMessage() {
61 $file = $this->getFile();
62 $line = $this->getLine();
63 $message = $this->getMessage();
64 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
67 function reportHTML() {
69 if ( $this->useOutputPage() ) {
70 $wgOut->setPageTitle( $this->getPageTitle() );
71 $wgOut->setRobotpolicy( "noindex,nofollow" );
72 $wgOut->setArticleRelated( false );
73 $wgOut->enableClientCache( false );
74 $wgOut->redirect( '' );
76 $wgOut->addHTML( $this->getHTML() );
79 echo $this->htmlHeader();
80 echo $this->getHTML();
81 echo $this->htmlFooter();
85 function reportText() {
86 echo $this->getText();
90 global $wgCommandLineMode;
91 if ( $wgCommandLineMode ) {
94 $log = $this->getLogMessage();
96 wfDebugLog( 'exception', $log );
102 function htmlHeader() {
103 global $wgLogo, $wgSitename, $wgOutputEncoding;
105 if ( !headers_sent() ) {
106 header( 'HTTP/1.0 500 Internal Server Error' );
107 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
108 /* Don't cache error pages! They cause no end of trouble... */
109 header( 'Cache-control: none' );
110 header( 'Pragma: nocache' );
112 $title = $this->getPageTitle();
115 <title>$title</title>
118 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
122 function htmlFooter() {
123 echo "</body></html>";
129 * Exception class which takes an HTML error message, and does not
130 * produce a backtrace. Replacement for OutputPage::fatalError().
131 * @addtogroup Exception
133 class FatalError
extends MWException
{
135 return $this->getMessage();
139 return $this->getMessage();
144 * @addtogroup Exception
146 class ErrorPageError
extends MWException
{
150 * Note: these arguments are keys into wfMsg(), not text!
152 function __construct( $title, $msg ) {
153 $this->title
= $title;
155 parent
::__construct( wfMsg( $msg ) );
160 $wgOut->showErrorPage( $this->title
, $this->msg
);
166 * Install an exception handler for MediaWiki exception types.
168 function wfInstallExceptionHandler() {
169 set_exception_handler( 'wfExceptionHandler' );
173 * Report an exception to the user
175 function wfReportException( Exception
$e ) {
176 if ( $e instanceof MWException
) {
179 } catch ( Exception
$e2 ) {
180 // Exception occurred from within exception handler
181 // Show a simpler error message for the original exception,
182 // don't try to invoke report()
183 $message = "MediaWiki internal error.\n\n" .
184 "Original exception: " . $e->__toString() .
185 "\n\nException caught inside exception handler: " .
186 $e2->__toString() . "\n";
188 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
191 echo nl2br( htmlspecialchars( $message ) ). "\n";
195 echo $e->__toString();
200 * Exception handler which simulates the appropriate catch() handling:
204 * } catch ( MWException $e ) {
206 * } catch ( Exception $e ) {
207 * echo $e->__toString();
210 function wfExceptionHandler( $e ) {
211 global $wgFullyInitialised;
212 wfReportException( $e );
214 // Final cleanup, similar to wfErrorExit()
215 if ( $wgFullyInitialised ) {
217 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
218 } catch ( Exception
$e ) {}
221 // Exit value should be nonzero for the benefit of shell jobs