Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / includes / exception / MWExceptionHandler.php
index bf232e9..78a5df9 100644 (file)
@@ -51,7 +51,7 @@ class MWExceptionHandler {
         * Install handlers with PHP.
         */
        public static function installHandler() {
-               set_exception_handler( 'MWExceptionHandler::handleException' );
+               set_exception_handler( 'MWExceptionHandler::handleUncaughtException' );
                set_error_handler( 'MWExceptionHandler::handleError' );
 
                // Reserve 16k of memory so we can report OOM fatals
@@ -111,6 +111,25 @@ class MWExceptionHandler {
                self::logException( $e, self::CAUGHT_BY_HANDLER );
        }
 
+       /**
+        * Callback to use with PHP's set_exception_handler.
+        *
+        * @since 1.31
+        * @param Exception|Throwable $e
+        */
+       public static function handleUncaughtException( $e ) {
+               self::handleException( $e );
+
+               // Make sure we don't claim success on exit for CLI scripts (T177414)
+               if ( wfIsCLI() ) {
+                       register_shutdown_function(
+                               function () {
+                                       exit( 255 );
+                               }
+                       );
+               }
+       }
+
        /**
         * Exception handler which simulates the appropriate catch() handling:
         *
@@ -151,6 +170,8 @@ class MWExceptionHandler {
        public static function handleError(
                $level, $message, $file = null, $line = null
        ) {
+               global $wgPropagateErrors;
+
                if ( in_array( $level, self::$fatalErrorTypes ) ) {
                        return call_user_func_array(
                                'MWExceptionHandler::handleFatalError', func_get_args()
@@ -194,9 +215,10 @@ class MWExceptionHandler {
                $e = new ErrorException( "PHP $levelName: $message", 0, $level, $file, $line );
                self::logError( $e, 'error', $severity );
 
-               // This handler is for logging only. Return false will instruct PHP
-               // to continue regular handling.
-               return false;
+               // If $wgPropagateErrors is true return false so PHP shows/logs the error normally.
+               // Ignore $wgPropagateErrors if the error should break execution, or track_errors is set
+               // (which means someone is counting on regular PHP error handling behavior).
+               return !( $wgPropagateErrors || $level == E_RECOVERABLE_ERROR || ini_get( 'track_errors' ) );
        }
 
        /**
@@ -260,7 +282,7 @@ class MWExceptionHandler {
                // HHVM: Class undefined: foo
                // PHP5: Class 'foo' not found
                if ( preg_match( "/Class (undefined: \w+|'\w+' not found)/", $msg ) ) {
-                       // @codingStandardsIgnoreStart Generic.Files.LineLength.TooLong
+                       // phpcs:disable Generic.Files.LineLength
                        $msg = <<<TXT
 {$msg}
 
@@ -268,7 +290,7 @@ MediaWiki or an installed extension requires this class but it is not embedded d
 
 Please see <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a> for help on installing the required components.
 TXT;
-                       // @codingStandardsIgnoreEnd
+                       // phpcs:enable
                }
 
                // We can't just create an exception and log it as it is likely that
@@ -281,7 +303,7 @@ TXT;
                $logger = LoggerFactory::getInstance( 'fatal' );
                $logger->error( $msg, [
                        'fatal_exception' => [
-                               'class' => 'ErrorException',
+                               'class' => ErrorException::class,
                                'message' => "PHP Fatal Error: {$message}",
                                'code' => $level,
                                'file' => $file,
@@ -441,6 +463,24 @@ TXT;
                return "[$id] $url   $type from line $line of $file: $message";
        }
 
+       /**
+        * Get a normalised message for formatting with PSR-3 log event context.
+        *
+        * Must be used together with `getLogContext()` to be useful.
+        *
+        * @since 1.30
+        * @param Exception|Throwable $e
+        * @return string
+        */
+       public static function getLogNormalMessage( $e ) {
+               $type = get_class( $e );
+               $file = $e->getFile();
+               $line = $e->getLine();
+               $message = $e->getMessage();
+
+               return "[{exception_id}] {exception_url}   $type from line $line of $file: $message";
+       }
+
        /**
         * @param Exception|Throwable $e
         * @return string
@@ -468,6 +508,7 @@ TXT;
                return [
                        'exception' => $e,
                        'exception_id' => WebRequest::getRequestId(),
+                       'exception_url' => self::getURL() ?: '[no req]',
                        'caught_by' => $catcher
                ];
        }
@@ -595,7 +636,7 @@ TXT;
                if ( !( $e instanceof MWException ) || $e->isLoggable() ) {
                        $logger = LoggerFactory::getInstance( 'exception' );
                        $logger->error(
-                               self::getLogMessage( $e ),
+                               self::getLogNormalMessage( $e ),
                                self::getLogContext( $e, $catcher )
                        );
 
@@ -623,13 +664,13 @@ TXT;
                $catcher = self::CAUGHT_BY_HANDLER;
                // The set_error_handler callback is independent from error_reporting.
                // Filter out unwanted errors manually (e.g. when
-               // MediaWiki\suppressWarnings is active).
+               // Wikimedia\suppressWarnings is active).
                $suppressed = ( error_reporting() & $e->getSeverity() ) === 0;
                if ( !$suppressed ) {
                        $logger = LoggerFactory::getInstance( $channel );
                        $logger->log(
                                $level,
-                               self::getLogMessage( $e ),
+                               self::getLogNormalMessage( $e ),
                                self::getLogContext( $e, $catcher )
                        );
                }