Merge "Cleanup some incorrect return annotations"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 1073de0..f0b48d5 100644 (file)
@@ -22,6 +22,7 @@
  * @author Trevor Parscal
  */
 
+use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
@@ -239,7 +240,7 @@ class ResourceLoader implements LoggerAwareInterface {
 
                if ( !$config ) {
                        $this->logger->debug( __METHOD__ . ' was called without providing a Config instance' );
-                       $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+                       $config = MediaWikiServices::getInstance()->getMainConfig();
                }
                $this->config = $config;
 
@@ -390,8 +391,6 @@ class ResourceLoader implements LoggerAwareInterface {
                }
        }
 
-       /**
-        */
        public function registerTestModules() {
                global $IP;
 
@@ -606,6 +605,25 @@ class ResourceLoader implements LoggerAwareInterface {
                return Wikimedia\base_convert( $hash, 16, 36, 7 );
        }
 
+       /**
+        * Add an error to the 'errors' array and log it.
+        *
+        * Should only be called from within respond().
+        *
+        * @since 1.29
+        * @param Exception $e
+        * @param string $msg
+        * @param array $context
+        */
+       protected function outputErrorAndLog( Exception $e, $msg, array $context = [] ) {
+               MWExceptionHandler::logException( $e );
+               $this->logger->warning(
+                       $msg,
+                       $context + [ 'exception' => $e ]
+               );
+               $this->errors[] = self::formatExceptionNoComment( $e );
+       }
+
        /**
         * Helper method to get and combine versions of multiple modules.
         *
@@ -619,7 +637,20 @@ class ResourceLoader implements LoggerAwareInterface {
                        return '';
                }
                $hashes = array_map( function ( $module ) use ( $context ) {
-                       return $this->getModule( $module )->getVersionHash( $context );
+                       try {
+                               return $this->getModule( $module )->getVersionHash( $context );
+                       } catch ( Exception $e ) {
+                               // If modules fail to compute a version, do still consider the versions
+                               // of other modules - don't set an empty string E-Tag for the whole request.
+                               // See also T152266 and StartupModule::getModuleRegistrations().
+                               $this->outputErrorAndLog( $e,
+                                       'Calculating version for "{module}" failed: {exception}',
+                                       [
+                                               'module' => $module,
+                                       ]
+                               );
+                               return '';
+                       }
                }, $moduleNames );
                return self::makeHash( implode( '', $hashes ) );
        }
@@ -694,11 +725,7 @@ class ResourceLoader implements LoggerAwareInterface {
                        // Preload for getCombinedVersion() and for batch makeModuleResponse()
                        $this->preloadModuleInfo( array_keys( $modules ), $context );
                } catch ( Exception $e ) {
-                       MWExceptionHandler::logException( $e );
-                       $this->logger->warning( 'Preloading module info failed: {exception}', [
-                               'exception' => $e
-                       ] );
-                       $this->errors[] = self::formatExceptionNoComment( $e );
+                       $this->outputErrorAndLog( $e, 'Preloading module info failed: {exception}' );
                }
 
                // Combine versions to propagate cache invalidation
@@ -706,11 +733,7 @@ class ResourceLoader implements LoggerAwareInterface {
                try {
                        $versionHash = $this->getCombinedVersion( $context, array_keys( $modules ) );
                } catch ( Exception $e ) {
-                       MWExceptionHandler::logException( $e );
-                       $this->logger->warning( 'Calculating version hash failed: {exception}', [
-                               'exception' => $e
-                       ] );
-                       $this->errors[] = self::formatExceptionNoComment( $e );
+                       $this->outputErrorAndLog( $e, 'Calculating version hash failed: {exception}' );
                }
 
                // See RFC 2616 ยง 3.11 Entity Tags
@@ -959,7 +982,9 @@ class ResourceLoader implements LoggerAwareInterface {
                        return MWExceptionHandler::getPublicLogMessage( $e );
                }
 
-               return MWExceptionHandler::getLogMessage( $e );
+               return MWExceptionHandler::getLogMessage( $e ) .
+                       "\nBacktrace:\n" .
+                       MWExceptionHandler::getRedactedTraceAsString( $e );
        }
 
        /**
@@ -1060,11 +1085,7 @@ MESSAGE;
                                $out .= $strContent;
 
                        } catch ( Exception $e ) {
-                               MWExceptionHandler::logException( $e );
-                               $this->logger->warning( 'Generating module package failed: {exception}', [
-                                       'exception' => $e
-                               ] );
-                               $this->errors[] = self::formatExceptionNoComment( $e );
+                               $this->outputErrorAndLog( $e, 'Generating module package failed: {exception}' );
 
                                // Respond to client with error-state instead of module implementation
                                $states[$name] = 'error';