- if ( $e instanceof MWException ) {
- try {
- $e->report();
- } catch ( Exception $e2 ) {
- // Exception occurred from within exception handler
- // Show a simpler error message for the original exception,
- // don't try to invoke report()
- $message = "MediaWiki internal error.\n\n" .
- "Original exception: " . $e->__toString() .
- "\n\nException caught inside exception handler: " .
- $e2->__toString() . "\n";
-
- if ( !empty( $GLOBALS['wgCommandLineMode'] ) ) {
- fwrite( STDERR, $message );
- } else {
- echo nl2br( htmlspecialchars( $message ) ). "\n";
- }
- }
- } else {
- echo $e->__toString();
- }
+ global $wgShowExceptionDetails;
+
+ $cmdLine = MWException::isCommandLine();
+
+ if ( $e instanceof MWException ) {
+ try {
+ $e->report();
+ } catch ( Exception $e2 ) {
+ // Exception occurred from within exception handler
+ // Show a simpler error message for the original exception,
+ // don't try to invoke report()
+ $message = "MediaWiki internal error.\n\n";
+
+ if ( $wgShowExceptionDetails ) {
+ $message .= 'Original exception: ' . $e->__toString() . "\n\n" .
+ 'Exception caught inside exception handler: ' . $e2->__toString();
+ } else {
+ $message .= "Exception caught inside exception handler.\n\n" .
+ "Set \$wgShowExceptionDetails = true; at the bottom of LocalSettings.php " .
+ "to show detailed debugging information.";
+ }
+
+ $message .= "\n";
+
+ if ( $cmdLine ) {
+ wfPrintError( $message );
+ } else {
+ echo nl2br( htmlspecialchars( $message ) ) . "\n";
+ }
+ }
+ } else {
+ $message = "Unexpected non-MediaWiki exception encountered, of type \"" . get_class( $e ) . "\"\n" .
+ $e->__toString() . "\n";
+
+ if ( $wgShowExceptionDetails ) {
+ $message .= "\n" . $e->getTraceAsString() . "\n";
+ }
+
+ if ( $cmdLine ) {
+ wfPrintError( $message );
+ } else {
+ echo nl2br( htmlspecialchars( $message ) ) . "\n";
+ }
+ }
+}
+
+/**
+ * Print a message, if possible to STDERR.
+ * Use this in command line mode only (see isCommandLine)
+ */
+function wfPrintError( $message ) {
+ # NOTE: STDERR may not be available, especially if php-cgi is used from the command line (bug #15602).
+ # Try to produce meaningful output anyway. Using echo may corrupt output to STDOUT though.
+ if ( defined( 'STDERR' ) ) {
+ fwrite( STDERR, $message );
+ } else {
+ echo( $message );
+ }