3 class MWException
extends Exception
5 function useOutputPage() {
6 return !empty( $GLOBALS['wgFullyInitialised'] );
9 function useMessageCache() {
11 return is_object( $wgLang );
14 function msg( $key, $fallback /*[, params...] */ ) {
15 $args = array_slice( func_get_args(), 2 );
16 if ( $this->useMessageCache() ) {
17 return wfMsgReal( $key, $args );
19 return wfMsgReplaceArgs( $fallback, $args );
24 return '<p>' . htmlspecialchars( $this->getMessage() ) .
25 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
30 return $this->getMessage() .
31 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
34 function getPageTitle() {
35 if ( $this->useMessageCache() ) {
36 return wfMsg( 'internalerror' );
39 return "$wgSitename error";
43 function reportHTML() {
45 if ( $this->useOutputPage() ) {
46 $wgOut->setPageTitle( $this->getPageTitle() );
47 $wgOut->setRobotpolicy( "noindex,nofollow" );
48 $wgOut->setArticleRelated( false );
49 $wgOut->enableClientCache( false );
50 $wgOut->redirect( '' );
52 $wgOut->addHTML( $this->getHTML() );
55 echo $this->htmlHeader();
56 echo $this->getHTML();
57 echo $this->htmlFooter();
61 function reportText() {
62 echo $this->getText();
66 global $wgCommandLineMode;
67 if ( $wgCommandLineMode ) {
74 function htmlHeader() {
75 global $wgLogo, $wgSitename, $wgOutputEncoding;
77 if ( !headers_sent() ) {
78 header( 'HTTP/1.0 500 Internal Server Error' );
79 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
80 /* Don't cache error pages! They cause no end of trouble... */
81 header( 'Cache-control: none' );
82 header( 'Pragma: nocache' );
84 $title = $this->getPageTitle();
90 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
94 function htmlFooter() {
95 echo "</body></html>";
100 * Exception class which takes an HTML error message, and does not
101 * produce a backtrace. Replacement for OutputPage::fatalError().
103 class FatalError
extends MWException
{
105 return $this->getMessage();
109 return $this->getMessage();
113 class ErrorPageError
extends MWException
{
117 * Note: these arguments are keys into wfMsg(), not text!
119 function __construct( $title, $msg ) {
120 $this->title
= $title;
122 parent
::__construct( wfMsg( $msg ) );
127 $wgOut->showErrorPage( $this->title
, $this->msg
);
133 * Install an exception handler for MediaWiki exception types.
135 function wfInstallExceptionHandler() {
136 set_exception_handler( 'wfExceptionHandler' );
140 * Report an exception to the user
142 function wfReportException( Exception
$e ) {
143 if ( is_a( $e, 'MWException' ) ) {
146 } catch ( Exception
$e2 ) {
147 // Exception occurred from within exception handler
148 // Show a simpler error message for the original exception,
149 // don't try to invoke report()
150 $message = "MediaWiki internal error.\n\n" .
151 "Original exception: " . $e->__toString() .
152 "\n\nException caught inside exception handler: " .
153 $e2->__toString() . "\n";
155 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
158 echo nl2br( htmlspecialchars( $message ) ). "\n";
162 echo $e->__toString();
167 * Exception handler which simulates the appropriate catch() handling:
171 * } catch ( MWException $e ) {
173 * } catch ( Exception $e ) {
174 * echo $e->__toString();
177 function wfExceptionHandler( $e ) {
178 global $wgFullyInitialised;
179 wfReportException( $e );
181 // Final cleanup, similar to wfErrorExit()
182 if ( $wgFullyInitialised ) {
185 logProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
186 } catch ( Exception
$e ) {}
189 // Exit value should be nonzero for the benefit of shell jobs