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 return '<p>' . htmlspecialchars( $this->getMessage() ) .
26 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
31 return $this->getMessage() .
32 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
35 function getPageTitle() {
36 if ( $this->useMessageCache() ) {
37 return wfMsg( 'internalerror' );
40 return "$wgSitename error";
44 function getLogMessage() {
45 $file = $this->getFile();
46 $line = $this->getLine();
47 $message = $this->getMessage();
48 return "{$_SERVER['REQUEST_URI']} Exception from line $line of $file: $message";
51 function reportHTML() {
53 if ( $this->useOutputPage() ) {
54 $wgOut->setPageTitle( $this->getPageTitle() );
55 $wgOut->setRobotpolicy( "noindex,nofollow" );
56 $wgOut->setArticleRelated( false );
57 $wgOut->enableClientCache( false );
58 $wgOut->redirect( '' );
60 $wgOut->addHTML( $this->getHTML() );
63 echo $this->htmlHeader();
64 echo $this->getHTML();
65 echo $this->htmlFooter();
69 function reportText() {
70 echo $this->getText();
74 global $wgCommandLineMode;
75 if ( $wgCommandLineMode ) {
78 $log = $this->getLogMessage();
80 wfDebugLog( 'exception', $log );
86 function htmlHeader() {
87 global $wgLogo, $wgSitename, $wgOutputEncoding;
89 if ( !headers_sent() ) {
90 header( 'HTTP/1.0 500 Internal Server Error' );
91 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
92 /* Don't cache error pages! They cause no end of trouble... */
93 header( 'Cache-control: none' );
94 header( 'Pragma: nocache' );
96 $title = $this->getPageTitle();
102 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
106 function htmlFooter() {
107 echo "</body></html>";
113 * Exception class which takes an HTML error message, and does not
114 * produce a backtrace. Replacement for OutputPage::fatalError().
116 class FatalError
extends MWException
{
118 return $this->getMessage();
122 return $this->getMessage();
126 class ErrorPageError
extends MWException
{
130 * Note: these arguments are keys into wfMsg(), not text!
132 function __construct( $title, $msg ) {
133 $this->title
= $title;
135 parent
::__construct( wfMsg( $msg ) );
140 $wgOut->showErrorPage( $this->title
, $this->msg
);
146 * Install an exception handler for MediaWiki exception types.
148 function wfInstallExceptionHandler() {
149 set_exception_handler( 'wfExceptionHandler' );
153 * Report an exception to the user
155 function wfReportException( Exception
$e ) {
156 if ( is_a( $e, 'MWException' ) ) {
159 } catch ( Exception
$e2 ) {
160 // Exception occurred from within exception handler
161 // Show a simpler error message for the original exception,
162 // don't try to invoke report()
163 $message = "MediaWiki internal error.\n\n" .
164 "Original exception: " . $e->__toString() .
165 "\n\nException caught inside exception handler: " .
166 $e2->__toString() . "\n";
168 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
171 echo nl2br( htmlspecialchars( $message ) ). "\n";
175 echo $e->__toString();
180 * Exception handler which simulates the appropriate catch() handling:
184 * } catch ( MWException $e ) {
186 * } catch ( Exception $e ) {
187 * echo $e->__toString();
190 function wfExceptionHandler( $e ) {
191 global $wgFullyInitialised;
192 wfReportException( $e );
194 // Final cleanup, similar to wfErrorExit()
195 if ( $wgFullyInitialised ) {
197 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
198 } catch ( Exception
$e ) {}
201 // Exit value should be nonzero for the benefit of shell jobs