3 class MWException
extends Exception
5 function useOutputPage() {
6 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
7 !empty( $GLOBALS['wgArticle'] ) && !empty( $GLOBALS['wgTitle'] );
10 function useMessageCache() {
12 return is_object( $wgLang );
15 function msg( $key, $fallback /*[, params...] */ ) {
16 $args = array_slice( func_get_args(), 2 );
17 if ( $this->useMessageCache() ) {
18 return wfMsgReal( $key, $args );
20 return wfMsgReplaceArgs( $fallback, $args );
25 global $wgShowExceptionDetails;
26 if( $wgShowExceptionDetails ) {
27 return '<p>' . htmlspecialchars( $this->getMessage() ) .
28 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
31 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
32 "in LocalSettings.php to show detailed debugging information.</p>";
37 global $wgShowExceptionDetails;
38 if( $wgShowExceptionDetails ) {
39 return $this->getMessage() .
40 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
42 return "<p>Set <tt>\$wgShowExceptionDetails = true;</tt> " .
43 "in LocalSettings.php to show detailed debugging information.</p>";
47 function getPageTitle() {
48 if ( $this->useMessageCache() ) {
49 return wfMsg( 'internalerror' );
52 return "$wgSitename error";
56 function getLogMessage() {
57 $file = $this->getFile();
58 $line = $this->getLine();
59 $message = $this->getMessage();
60 return "{$_SERVER['REQUEST_URI']} Exception from line $line of $file: $message";
63 function reportHTML() {
65 if ( $this->useOutputPage() ) {
66 $wgOut->setPageTitle( $this->getPageTitle() );
67 $wgOut->setRobotpolicy( "noindex,nofollow" );
68 $wgOut->setArticleRelated( false );
69 $wgOut->enableClientCache( false );
70 $wgOut->redirect( '' );
72 $wgOut->addHTML( $this->getHTML() );
75 echo $this->htmlHeader();
76 echo $this->getHTML();
77 echo $this->htmlFooter();
81 function reportText() {
82 echo $this->getText();
86 global $wgCommandLineMode;
87 if ( $wgCommandLineMode ) {
90 $log = $this->getLogMessage();
92 wfDebugLog( 'exception', $log );
98 function htmlHeader() {
99 global $wgLogo, $wgSitename, $wgOutputEncoding;
101 if ( !headers_sent() ) {
102 header( 'HTTP/1.0 500 Internal Server Error' );
103 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
104 /* Don't cache error pages! They cause no end of trouble... */
105 header( 'Cache-control: none' );
106 header( 'Pragma: nocache' );
108 $title = $this->getPageTitle();
111 <title>$title</title>
114 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
118 function htmlFooter() {
119 echo "</body></html>";
125 * Exception class which takes an HTML error message, and does not
126 * produce a backtrace. Replacement for OutputPage::fatalError().
128 class FatalError
extends MWException
{
130 return $this->getMessage();
134 return $this->getMessage();
138 class ErrorPageError
extends MWException
{
142 * Note: these arguments are keys into wfMsg(), not text!
144 function __construct( $title, $msg ) {
145 $this->title
= $title;
147 parent
::__construct( wfMsg( $msg ) );
152 $wgOut->showErrorPage( $this->title
, $this->msg
);
158 * Install an exception handler for MediaWiki exception types.
160 function wfInstallExceptionHandler() {
161 set_exception_handler( 'wfExceptionHandler' );
165 * Report an exception to the user
167 function wfReportException( Exception
$e ) {
168 if ( $e instanceof MWException
) {
171 } catch ( Exception
$e2 ) {
172 // Exception occurred from within exception handler
173 // Show a simpler error message for the original exception,
174 // don't try to invoke report()
175 $message = "MediaWiki internal error.\n\n" .
176 "Original exception: " . $e->__toString() .
177 "\n\nException caught inside exception handler: " .
178 $e2->__toString() . "\n";
180 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
183 echo nl2br( htmlspecialchars( $message ) ). "\n";
187 echo $e->__toString();
192 * Exception handler which simulates the appropriate catch() handling:
196 * } catch ( MWException $e ) {
198 * } catch ( Exception $e ) {
199 * echo $e->__toString();
202 function wfExceptionHandler( $e ) {
203 global $wgFullyInitialised;
204 wfReportException( $e );
206 // Final cleanup, similar to wfErrorExit()
207 if ( $wgFullyInitialised ) {
209 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
210 } catch ( Exception
$e ) {}
213 // Exit value should be nonzero for the benefit of shell jobs