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 reportHTML() {
46 if ( $this->useOutputPage() ) {
47 $wgOut->setPageTitle( $this->getPageTitle() );
48 $wgOut->setRobotpolicy( "noindex,nofollow" );
49 $wgOut->setArticleRelated( false );
50 $wgOut->enableClientCache( false );
51 $wgOut->redirect( '' );
53 $wgOut->addHTML( $this->getHTML() );
56 echo $this->htmlHeader();
57 echo $this->getHTML();
58 echo $this->htmlFooter();
62 function reportText() {
63 echo $this->getText();
67 global $wgCommandLineMode;
68 if ( $wgCommandLineMode ) {
75 function htmlHeader() {
76 global $wgLogo, $wgSitename, $wgOutputEncoding;
78 if ( !headers_sent() ) {
79 header( 'HTTP/1.0 500 Internal Server Error' );
80 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
81 /* Don't cache error pages! They cause no end of trouble... */
82 header( 'Cache-control: none' );
83 header( 'Pragma: nocache' );
85 $title = $this->getPageTitle();
91 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
95 function htmlFooter() {
96 echo "</body></html>";
101 * Exception class which takes an HTML error message, and does not
102 * produce a backtrace. Replacement for OutputPage::fatalError().
104 class FatalError
extends MWException
{
106 return $this->getMessage();
110 return $this->getMessage();
114 class ErrorPageError
extends MWException
{
118 * Note: these arguments are keys into wfMsg(), not text!
120 function __construct( $title, $msg ) {
121 $this->title
= $title;
123 parent
::__construct( wfMsg( $msg ) );
128 $wgOut->showErrorPage( $this->title
, $this->msg
);
134 * Install an exception handler for MediaWiki exception types.
136 function wfInstallExceptionHandler() {
137 set_exception_handler( 'wfExceptionHandler' );
141 * Report an exception to the user
143 function wfReportException( Exception
$e ) {
144 if ( is_a( $e, 'MWException' ) ) {
147 } catch ( Exception
$e2 ) {
148 // Exception occurred from within exception handler
149 // Show a simpler error message for the original exception,
150 // don't try to invoke report()
151 $message = "MediaWiki internal error.\n\n" .
152 "Original exception: " . $e->__toString() .
153 "\n\nException caught inside exception handler: " .
154 $e2->__toString() . "\n";
156 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
159 echo nl2br( htmlspecialchars( $message ) ). "\n";
163 echo $e->__toString();
168 * Exception handler which simulates the appropriate catch() handling:
172 * } catch ( MWException $e ) {
174 * } catch ( Exception $e ) {
175 * echo $e->__toString();
178 function wfExceptionHandler( $e ) {
179 global $wgFullyInitialised;
180 wfReportException( $e );
182 // Final cleanup, similar to wfErrorExit()
183 if ( $wgFullyInitialised ) {
185 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
186 } catch ( Exception
$e ) {}
189 // Exit value should be nonzero for the benefit of shell jobs