3 * @defgroup Exception Exception
10 class MWException
extends Exception
{
13 * Should the exception use $wgOut to output the error ?
16 function useOutputPage() {
17 return !empty( $GLOBALS['wgFullyInitialised'] ) &&
18 ( !empty( $GLOBALS['wgArticle'] ) ||
( !empty( $GLOBALS['wgOut'] ) && !$GLOBALS['wgOut']->isArticle() ) ) &&
19 !empty( $GLOBALS['wgTitle'] );
23 * Can the extension use wfMsg() to get i18n messages ?
26 function useMessageCache() {
28 return is_object( $wgLang );
32 * Run hook to allow extensions to modify the text of the exception
34 * @param String $name class name of the exception
35 * @param Array $args arguments to pass to the callback functions
36 * @return mixed string to output or null if any hook has been called
38 function runHooks( $name, $args = array() ) {
39 global $wgExceptionHooks;
40 if( !isset( $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks ) )
41 return; // Just silently ignore
42 if( !array_key_exists( $name, $wgExceptionHooks ) ||
!is_array( $wgExceptionHooks[ $name ] ) )
44 $hooks = $wgExceptionHooks[ $name ];
45 $callargs = array_merge( array( $this ), $args );
47 foreach( $hooks as $hook ) {
48 if( is_string( $hook ) ||
( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { //'function' or array( 'class', hook' )
49 $result = call_user_func_array( $hook, $callargs );
53 if( is_string( $result ) )
59 * Get a message from i18n
61 * @param String $key message name
62 * @param String $fallback default message if the message cache can't be
63 * called by the exception
64 * The function also has other parameters that are arguments for the message
65 * @return String message with arguments replaced
67 function msg( $key, $fallback /*[, params...] */ ) {
68 $args = array_slice( func_get_args(), 2 );
69 if ( $this->useMessageCache() ) {
70 return wfMsgReal( $key, $args );
72 return wfMsgReplaceArgs( $fallback, $args );
77 * If $wgShowExceptionDetails is true, return a HTML message with a
78 * backtrace to the error, otherwise show a message to ask to set it to true
79 * to show that information.
81 * @return String html to output
84 global $wgShowExceptionDetails;
85 if( $wgShowExceptionDetails ) {
86 return '<p>' . htmlspecialchars( $this->getMessage() ) .
87 '</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
90 return "<p>Set <b><tt>\$wgShowExceptionDetails = true;</tt></b> " .
91 "at the bottom of LocalSettings.php to show detailed " .
92 "debugging information.</p>";
97 * If $wgShowExceptionDetails is true, return a text message with a
98 * backtrace to the error.
101 global $wgShowExceptionDetails;
102 if( $wgShowExceptionDetails ) {
103 return $this->getMessage() .
104 "\nBacktrace:\n" . $this->getTraceAsString() . "\n";
106 return "Set \$wgShowExceptionDetails = true; " .
107 "in LocalSettings.php to show detailed debugging information.\n";
111 /* Return titles of this error page */
112 function getPageTitle() {
113 if ( $this->useMessageCache() ) {
114 return wfMsg( 'internalerror' );
117 return "$wgSitename error";
122 * Return the requested URL and point to file and line number from which the
127 function getLogMessage() {
129 $file = $this->getFile();
130 $line = $this->getLine();
131 $message = $this->getMessage();
132 return $wgRequest->getRequestURL() . " Exception from line $line of $file: $message";
135 /** Output the exception report using HTML */
136 function reportHTML() {
138 if ( $this->useOutputPage() ) {
139 $wgOut->setPageTitle( $this->getPageTitle() );
140 $wgOut->setRobotpolicy( "noindex,nofollow" );
141 $wgOut->setArticleRelated( false );
142 $wgOut->enableClientCache( false );
143 $wgOut->redirect( '' );
145 if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
146 $wgOut->addHTML( $hookResult );
148 $wgOut->addHTML( $this->getHTML() );
152 if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
155 echo $this->htmlHeader();
156 echo $this->getHTML();
157 echo $this->htmlFooter();
162 * Output a report about the exception and takes care of formatting.
163 * It will be either HTML or plain text based on $wgCommandLineMode.
166 global $wgCommandLineMode;
167 $log = $this->getLogMessage();
169 wfDebugLog( 'exception', $log );
171 if ( $wgCommandLineMode ) {
172 fwrite( STDERR
, $this->getText() );
179 * Send headers and output the beginning of the html page if not using
180 * $wgOut to output the exception.
182 function htmlHeader() {
183 global $wgLogo, $wgSitename, $wgOutputEncoding;
185 if ( !headers_sent() ) {
186 header( 'HTTP/1.0 500 Internal Server Error' );
187 header( 'Content-type: text/html; charset='.$wgOutputEncoding );
188 /* Don't cache error pages! They cause no end of trouble... */
189 header( 'Cache-control: none' );
190 header( 'Pragma: nocache' );
192 $title = $this->getPageTitle();
195 <title>$title</title>
198 <h1><img src='$wgLogo' style='float:left;margin-right:1em' alt=''>$title</h1>
203 * print the end of the html page if not using $wgOut.
205 function htmlFooter() {
206 echo "</body></html>";
211 * Exception class which takes an HTML error message, and does not
212 * produce a backtrace. Replacement for OutputPage::fatalError().
215 class FatalError
extends MWException
{
217 return $this->getMessage();
221 return $this->getMessage();
228 class ErrorPageError
extends MWException
{
232 * Note: these arguments are keys into wfMsg(), not text!
234 function __construct( $title, $msg ) {
235 $this->title
= $title;
237 parent
::__construct( wfMsg( $msg ) );
242 $wgOut->showErrorPage( $this->title
, $this->msg
);
248 * Install an exception handler for MediaWiki exception types.
250 function wfInstallExceptionHandler() {
251 set_exception_handler( 'wfExceptionHandler' );
255 * Report an exception to the user
257 function wfReportException( Exception
$e ) {
258 if ( $e instanceof MWException
) {
261 } catch ( Exception
$e2 ) {
262 // Exception occurred from within exception handler
263 // Show a simpler error message for the original exception,
264 // don't try to invoke report()
265 $message = "MediaWiki internal error.\n\n" .
266 "Original exception: " . $e->__toString() .
267 "\n\nException caught inside exception handler: " .
268 $e2->__toString() . "\n";
270 if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
271 fwrite( STDERR
, $message );
273 echo nl2br( htmlspecialchars( $message ) ). "\n";
277 echo $e->__toString();
282 * Exception handler which simulates the appropriate catch() handling:
286 * } catch ( MWException $e ) {
288 * } catch ( Exception $e ) {
289 * echo $e->__toString();
292 function wfExceptionHandler( $e ) {
293 global $wgFullyInitialised;
294 wfReportException( $e );
296 // Final cleanup, similar to wfErrorExit()
297 if ( $wgFullyInitialised ) {
299 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
300 } catch ( Exception
$e ) {}
303 // Exit value should be nonzero for the benefit of shell jobs