Merge "Replace wgShowExceptionDetails with wgShowDBErrorBacktrace on db errors"
[lhc/web/wiklou.git] / includes / exception / MWExceptionRenderer.php
index bb7a01f..c0f1e84 100644 (file)
@@ -28,18 +28,13 @@ class MWExceptionRenderer {
        const AS_PRETTY = 2; // show as HTML
 
        /**
-        * @param Exception $e Original exception
+        * @param Exception|Throwable $e Original exception
         * @param integer $mode MWExceptionExposer::AS_* constant
-        * @param Exception|null $eNew New exception from attempting to show the first
+        * @param Exception|Throwable|null $eNew New exception from attempting to show the first
         */
-       public static function output( Exception $e, $mode, Exception $eNew = null ) {
+       public static function output( $e, $mode, $eNew = null ) {
                global $wgMimeType;
 
-               if ( $e instanceof DBConnectionError ) {
-                       self::reportOutageHTML( $e );
-                       return;
-               }
-
                if ( defined( 'MW_API' ) ) {
                        // Unhandled API exception, we can't be sure that format printer is alive
                        self::header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $e ) );
@@ -47,9 +42,13 @@ class MWExceptionRenderer {
                } elseif ( self::isCommandLine() ) {
                        self::printError( self::getText( $e ) );
                } elseif ( $mode === self::AS_PRETTY ) {
-                       self::statusHeader( 500 );
-                       self::header( "Content-Type: $wgMimeType; charset=utf-8" );
-                       self::reportHTML( $e );
+                       if ( $e instanceof DBConnectionError ) {
+                               self::reportOutageHTML( $e );
+                       } else {
+                               self::statusHeader( 500 );
+                               self::header( "Content-Type: $wgMimeType; charset=utf-8" );
+                               self::reportHTML( $e );
+                       }
                } else {
                        if ( $eNew ) {
                                $message = "MediaWiki internal error.\n\n";
@@ -62,8 +61,7 @@ class MWExceptionRenderer {
                                                "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $eNew );
                                } else {
                                        $message .= "Exception caught inside exception handler.\n\n" .
-                                               "Set \$wgShowExceptionDetails = true; at the bottom of LocalSettings.php " .
-                                               "to show detailed debugging information.";
+                                               self::getShowBacktraceError( $e );
                                }
                                $message .= "\n";
                        } else {
@@ -75,11 +73,7 @@ class MWExceptionRenderer {
                                        $message = MWExceptionHandler::getPublicLogMessage( $e );
                                }
                        }
-                       if ( self::isCommandLine() ) {
-                               self::printError( $message );
-                       } else {
-                               echo nl2br( htmlspecialchars( $message ) ) . "\n";
-                       }
+                       echo nl2br( htmlspecialchars( $message ) ) . "\n";
                }
        }
 
@@ -88,12 +82,12 @@ class MWExceptionRenderer {
         *
         * Called by MWException for b/c
         *
-        * @param Exception $e
+        * @param Exception|Throwable $e
         * @param string $name Class name of the exception
         * @param array $args Arguments to pass to the callback functions
         * @return string|null String to output or null if any hook has been called
         */
-       public static function runHooks( Exception $e, $name, $args = [] ) {
+       public static function runHooks( $e, $name, $args = [] ) {
                global $wgExceptionHooks;
 
                if ( !isset( $wgExceptionHooks ) || !is_array( $wgExceptionHooks ) ) {
@@ -129,10 +123,10 @@ class MWExceptionRenderer {
        }
 
        /**
-        * @param Exception $e
+        * @param Exception|Throwable $e
         * @return bool Should the exception use $wgOut to output the error?
         */
-       private static function useOutputPage( Exception $e ) {
+       private static function useOutputPage( $e ) {
                // Can the extension use the Message class/wfMessage to get i18n-ed messages?
                foreach ( $e->getTrace() as $frame ) {
                        if ( isset( $frame['class'] ) && $frame['class'] === 'LocalisationCache' ) {
@@ -150,9 +144,9 @@ class MWExceptionRenderer {
        /**
         * Output the exception report using HTML
         *
-        * @param Exception $e
+        * @param Exception|Throwable $e
         */
-       private static function reportHTML( Exception $e ) {
+       private static function reportHTML( $e ) {
                global $wgOut, $wgSitename;
 
                if ( self::useOutputPage( $e ) ) {
@@ -172,7 +166,7 @@ class MWExceptionRenderer {
                        } else {
                                // Show any custom GUI message before the details
                                if ( $e instanceof MessageSpecifier ) {
-                                       $wgOut->addHtml( Message::newFromSpecifier( $e )->escaped() );
+                                       $wgOut->addHTML( Message::newFromSpecifier( $e )->escaped() );
                                }
                                $wgOut->addHTML( self::getHTML( $e ) );
                        }
@@ -206,10 +200,10 @@ class MWExceptionRenderer {
         * backtrace to the error, otherwise show a message to ask to set it to true
         * to show that information.
         *
-        * @param Exception $e
+        * @param Exception|Throwable $e
         * @return string Html to output
         */
-       public static function getHTML( Exception $e ) {
+       public static function getHTML( $e ) {
                if ( self::showBackTrace( $e ) ) {
                        $html = "<div class=\"errorbox\"><p>" .
                                nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $e ) ) ) .
@@ -227,9 +221,7 @@ class MWExceptionRenderer {
                                        $logId,
                                        MWExceptionHandler::getURL()
                                ) . "</div>\n" .
-                       "<!-- Set \$wgShowExceptionDetails = true; " .
-                       "at the bottom of LocalSettings.php to show detailed " .
-                       "debugging information. -->";
+                               "<!-- " . wordwrap( self::getShowBacktraceError( $e ), 50 ) . " -->";
                }
 
                return $html;
@@ -254,25 +246,24 @@ class MWExceptionRenderer {
        }
 
        /**
-        * @param Exception $e
+        * @param Exception|Throwable $e
         * @return string
         */
-       private function getText( Exception $e ) {
+       private static function getText( $e ) {
                if ( self::showBackTrace( $e ) ) {
                        return MWExceptionHandler::getLogMessage( $e ) .
                                "\nBacktrace:\n" .
                                MWExceptionHandler::getRedactedTraceAsString( $e ) . "\n";
                } else {
-                       return "Set \$wgShowExceptionDetails = true; " .
-                               "in LocalSettings.php to show detailed debugging information.\n";
+                       return self::getShowBacktraceError( $e );
                }
        }
 
        /**
-        * @param Exception $e
+        * @param Exception|Throwable $e
         * @return bool
         */
-       private static function showBackTrace( Exception $e ) {
+       private static function showBackTrace( $e ) {
                global $wgShowExceptionDetails, $wgShowDBErrorBacktrace;
 
                return (
@@ -281,6 +272,23 @@ class MWExceptionRenderer {
                );
        }
 
+       /**
+        * @param Exception|Throwable $e
+        * @return string
+        */
+       private static function getShowBacktraceError( $e ) {
+               global $wgShowExceptionDetails, $wgShowDBErrorBacktrace;
+               $vars = [];
+               if ( !$wgShowExceptionDetails ) {
+                       $vars[] = '$wgShowExceptionDetails = true;';
+               }
+               if ( $e instanceof DBError && !$wgShowDBErrorBacktrace ) {
+                       $vars[] = '$wgShowDBErrorBacktrace = true;';
+               }
+               $vars = implode( ' and ', $vars );
+               return "Set $vars at the bottom of LocalSettings.php to show detailed debugging information";
+       }
+
        /**
         * @return bool
         */
@@ -324,9 +332,9 @@ class MWExceptionRenderer {
        }
 
        /**
-        * @param Exception $e
+        * @param Exception|Throwable $e
         */
-       private static function reportOutageHTML( Exception $e ) {
+       private static function reportOutageHTML( $e ) {
                global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
 
                $sorry = htmlspecialchars( self::msg(