resourceloader: Log load.php exceptions to JavaScript console
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 15 Sep 2015 03:17:47 +0000 (04:17 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Tue, 15 Sep 2015 03:20:30 +0000 (04:20 +0100)
* Use implode() instead of foreach concat.
* Use two new lines instead of just one since the exception message
  with stacktrace spans multiple lines (makes it easier to distinguish
  when there are indeed exceptions from multiple sources).
* Output a single comment instead of one for each.
* If context is a JavaScript response, also include a console.error
  call with the erro message.

To try out:
* Break a file module descriptor in Resources.php by e.g. making
  a typo in one of the scripts arrays.
* View a page on-wiki that uses the module (e.g. jquery.accessKeyLabel
  is loaded on most pages).
* Observe error in the console.

Bug: T110659
Change-Id: I4272795f1fab96a2effe2a6c068a56421adaa512

includes/resourceloader/ResourceLoader.php

index b8ec63b..fa0f23d 100644 (file)
@@ -747,18 +747,18 @@ class ResourceLoader implements LoggerAwareInterface {
 
                if ( $context->getImageObj() && $this->errors ) {
                        // We can't show both the error messages and the response when it's an image.
-                       $errorText = '';
-                       foreach ( $this->errors as $error ) {
-                               $errorText .= $error . "\n";
-                       }
-                       $response = $errorText;
+                       $response = implode( "\n\n",  $this->errors );
                } elseif ( $this->errors ) {
-                       // Prepend comments indicating errors
-                       $errorText = '';
-                       foreach ( $this->errors as $error ) {
-                               $errorText .= self::makeComment( $error );
+                       $errorText = implode( "\n\n", $this->errors );
+                       $errorResponse = self::makeComment( $errorText );
+                       if ( $context->shouldIncludeScripts() ) {
+                               $errorResponse .= 'if (window.console && console.error) {'
+                                       . Xml::encodeJsCall( 'console.error', array( $errorText ) )
+                                       . "}\n";
                        }
-                       $response = $errorText . $response;
+
+                       // Prepend error info to the response
+                       $response = $errorResponse . $response;
                }
 
                $this->errors = array();