X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fexception%2FMWExceptionHandler.php;h=f3d61f0cddb2f806eea11f732fc2c4d513a10f6b;hb=255d76f2a13a8378ded9f0cf1c2bb172f7f07a5b;hp=a2867a1879bab0f30e952e2456469e150703e1dd;hpb=64b83bdb3afd0ee4f8fc1893a865409c198e601e;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/exception/MWExceptionHandler.php b/includes/exception/MWExceptionHandler.php index a2867a1879..f3d61f0cdd 100644 --- a/includes/exception/MWExceptionHandler.php +++ b/includes/exception/MWExceptionHandler.php @@ -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 ( PHP_SAPI === 'cli' ) { + register_shutdown_function( + function () { + exit( 255 ); + } + ); + } + } + /** * Exception handler which simulates the appropriate catch() handling: * @@ -260,7 +279,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 = <<mediawiki.org 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 @@ -441,6 +460,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 +505,7 @@ TXT; return [ 'exception' => $e, 'exception_id' => WebRequest::getRequestId(), + 'exception_url' => self::getURL() ?: '[no req]', 'caught_by' => $catcher ]; } @@ -595,7 +633,7 @@ TXT; if ( !( $e instanceof MWException ) || $e->isLoggable() ) { $logger = LoggerFactory::getInstance( 'exception' ); $logger->error( - self::getLogMessage( $e ), + self::getLogNormalMessage( $e ), self::getLogContext( $e, $catcher ) ); @@ -616,7 +654,7 @@ TXT; * @param ErrorException $e * @param string $channel * @param string $level - */ + */ protected static function logError( ErrorException $e, $channel, $level = LogLevel::ERROR ) { @@ -629,7 +667,7 @@ TXT; $logger = LoggerFactory::getInstance( $channel ); $logger->log( $level, - self::getLogMessage( $e ), + self::getLogNormalMessage( $e ), self::getLogContext( $e, $catcher ) ); }