25use Wikimedia\NormalizedException\INormalizedException;
29use Wikimedia\Services\RecursiveServiceDependencyException;
37 public const CAUGHT_BY_HANDLER =
'mwe_handler';
39 public const CAUGHT_BY_ENTRYPOINT =
'entrypoint';
41 public const CAUGHT_BY_OTHER =
'other';
56 protected static $fatalErrorTypes = [
72 private static $logExceptionBacktrace =
true;
79 private static $propagateErrors;
89 bool $logExceptionBacktrace =
true,
90 bool $propagateErrors =
true
92 self::$logExceptionBacktrace = $logExceptionBacktrace;
93 self::$propagateErrors = $propagateErrors;
108 set_exception_handler(
'MWExceptionHandler::handleUncaughtException' );
112 set_error_handler(
'MWExceptionHandler::handleError' );
117 self::$reservedMemory = str_repeat(
' ', 16384 );
118 register_shutdown_function(
'MWExceptionHandler::handleFatalError' );
125 protected static function report( Throwable $e ) {
134 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_PRETTY );
136 }
catch ( Throwable $e2 ) {
140 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW, $e2 );
149 private static function rollbackPrimaryChanges() {
150 if ( !MediaWikiServices::hasInstance() ) {
156 $services = MediaWikiServices::getInstance();
157 $lbFactory = $services->peekService(
'DBLoadBalancerFactory' );
158 '@phan-var LBFactory $lbFactory';
169 $lbFactory->rollbackPrimaryChanges( __METHOD__ );
170 $lbFactory->flushPrimarySessions( __METHOD__ );
177 self::logException( $e, self::CAUGHT_BY_HANDLER );
192 $catcher = self::CAUGHT_BY_OTHER
194 self::rollbackPrimaryChanges();
196 self::logException( $e, $catcher );
206 $catcher = self::CAUGHT_BY_OTHER
209 self::rollbackPrimaryChangesAndLog( $e, $catcher );
219 self::handleException( $e, self::CAUGHT_BY_HANDLER );
223 register_shutdown_function(
249 public static function handleException( Throwable $e, $catcher = self::CAUGHT_BY_OTHER ) {
250 self::rollbackPrimaryChangesAndLog( $e, $catcher );
292 case E_COMPILE_WARNING:
293 $prefix =
'PHP Warning: ';
294 $severity = LogLevel::ERROR;
297 $prefix =
'PHP Notice: ';
298 $severity = LogLevel::ERROR;
302 $prefix =
'PHP Notice: ';
303 $severity = LogLevel::WARNING;
307 $prefix =
'PHP Warning: ';
308 $severity = LogLevel::WARNING;
311 $prefix =
'PHP Strict Standards: ';
312 $severity = LogLevel::WARNING;
315 $prefix =
'PHP Deprecated: ';
316 $severity = LogLevel::WARNING;
318 case E_USER_DEPRECATED:
319 $prefix =
'PHP Deprecated: ';
320 $severity = LogLevel::WARNING;
321 $real = MWDebug::parseCallerDescription( $message );
326 $file = $real[
'file'];
327 $line = $real[
'line'];
328 $message = $real[
'message'];
332 $prefix =
'PHP Unknown error: ';
333 $severity = LogLevel::ERROR;
338 $e =
new ErrorException( $prefix . $message, 0, $level,
$file,
$line );
339 self::logError( $e,
'error', $severity, self::CAUGHT_BY_HANDLER );
344 return !( self::$propagateErrors || ini_get(
'track_errors' ) );
364 self::$reservedMemory =
null;
366 $lastError = error_get_last();
367 if ( $lastError ===
null ) {
371 $level = $lastError[
'type'];
372 $message = $lastError[
'message'];
373 $file = $lastError[
'file'];
374 $line = $lastError[
'line'];
376 if ( !in_array( $level, self::$fatalErrorTypes ) ) {
383 '[{reqId}] {exception_url} PHP Fatal Error',
385 $line ?
" line $line" :
'',
387 $file ?
" $file" :
'',
390 $msg = implode(
'', $msgParts );
393 if ( preg_match(
"/Class '\w+' not found/", $message ) ) {
398MediaWiki or an installed extension
requires this class but it is not embedded directly in
MediaWiki's git repository and must be installed separately by the end user.
400Please see <a href=
"https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
for help on installing the required components.
405 $e =
new ErrorException(
"PHP Fatal Error: {$message}", 0, $level,
$file,
$line );
406 $logger = LoggerFactory::getInstance(
'exception' );
407 $logger->error( $msg, self::getLogContext( $e, self::CAUGHT_BY_HANDLER ) );
423 $from =
'from ' . $e->getFile() .
'(' . $e->getLine() .
')' .
"\n";
424 return $from . self::prettyPrintTrace( self::getRedactedTrace( $e ) );
439 foreach ( $trace as $level => $frame ) {
440 if ( isset( $frame[
'file'] ) && isset( $frame[
'line'] ) ) {
441 $text .=
"{$pad}#{$level} {$frame['file']}({$frame['line']}): ";
447 $text .=
"{$pad}#{$level} [internal function]: ";
450 if ( isset( $frame[
'class'] ) && isset( $frame[
'type'] ) && isset( $frame[
'function'] ) ) {
451 $text .= $frame[
'class'] . $frame[
'type'] . $frame[
'function'];
453 $text .= $frame[
'function'] ??
'NO_FUNCTION_GIVEN';
456 if ( isset( $frame[
'args'] ) ) {
457 $text .=
'(' . implode(
', ', $frame[
'args'] ) .
")\n";
464 $text .=
"{$pad}#{$level} {main}";
481 return static::redactTrace( $e->getTrace() );
495 return array_map(
static function ( $frame ) {
496 if ( isset( $frame[
'args'] ) ) {
497 $frame[
'args'] = array_map(
static function ( $arg ) {
498 return is_object( $arg ) ? get_class( $arg ) : gettype( $arg );
532 $id = WebRequest::getRequestId();
533 $type = get_class( $e );
534 $message = $e->getMessage();
535 $url = self::getURL() ?:
'[no req]';
538 $message =
"A database query error has occurred. Did you forget to run"
539 .
" your application's database schema updater after upgrading"
540 .
" or after adding a new extension?\n\nPlease see"
541 .
" https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Upgrading and"
542 .
" https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:How_to_debug"
543 .
" for more information.\n\n"
547 return "[$id] $url $type: $message";
560 if ( $e instanceof INormalizedException ) {
561 $message = $e->getNormalizedMessage();
563 $message = $e->getMessage();
565 if ( !$e instanceof ErrorException ) {
570 $message = get_class( $e ) .
": $message";
573 return "[{reqId}] {exception_url} $message";
581 $reqId = WebRequest::getRequestId();
582 $type = get_class( $e );
583 return '[' . $reqId .
'] '
584 . gmdate(
'Y-m-d H:i:s' ) .
': '
585 .
'Fatal exception of type "' .
$type .
'"';
600 public static function getLogContext( Throwable $e, $catcher = self::CAUGHT_BY_OTHER ) {
603 'exception_url' => self::getURL() ?:
'[no req]',
610 'reqId' => WebRequest::getRequestId(),
611 'caught_by' => $catcher
613 if ( $e instanceof INormalizedException ) {
614 $context += $e->getMessageContext();
633 $catcher = self::CAUGHT_BY_OTHER
636 'id' => WebRequest::getRequestId(),
637 'type' => get_class( $e ),
638 'file' => $e->getFile(),
639 'line' => $e->getLine(),
640 'message' => $e->getMessage(),
641 'code' => $e->getCode(),
642 'url' => self::getURL() ?:
null,
643 'caught_by' => $catcher
646 if ( $e instanceof ErrorException &&
647 ( error_reporting() & $e->getSeverity() ) === 0
650 $data[
'suppressed'] =
true;
653 if ( self::$logExceptionBacktrace ) {
654 $data[
'backtrace'] = self::getRedactedTrace( $e );
657 $previous = $e->getPrevious();
658 if ( $previous !==
null ) {
659 $data[
'previous'] = self::getStructuredExceptionData( $previous, $catcher );
723 $catcher = self::CAUGHT_BY_OTHER
725 return FormatJson::encode(
726 self::getStructuredExceptionData( $e, $catcher ),
745 $catcher = self::CAUGHT_BY_OTHER,
748 if ( !( $e instanceof
MWException ) || $e->isLoggable() ) {
749 $logger = LoggerFactory::getInstance(
'exception' );
750 $context = self::getLogContext( $e, $catcher );
752 $context[
'extraData'] = $extraData;
755 self::getLogNormalMessage( $e ),
759 $json = self::jsonSerializeException( $e,
false, FormatJson::ALL_OK, $catcher );
760 if ( $json !==
false ) {
761 $logger = LoggerFactory::getInstance(
'exception-json' );
762 $logger->error( $json, [
'private' =>
true ] );
765 self::callLogExceptionHook( $e,
false );
777 private static function logError(
786 $suppressed = ( error_reporting() & $e->getSeverity() ) === 0;
787 if ( !$suppressed ) {
788 $logger = LoggerFactory::getInstance( $channel );
791 self::getLogNormalMessage( $e ),
792 self::getLogContext( $e, $catcher )
797 $json = self::jsonSerializeException( $e,
false, FormatJson::ALL_OK, $catcher );
798 if ( $json !==
false ) {
799 $logger = LoggerFactory::getInstance(
"{$channel}-json" );
806 $unfilteredLevel = $suppressed ? LogLevel::DEBUG : $level;
807 $logger->log( $unfilteredLevel, $json, [
'private' =>
true ] );
810 self::callLogExceptionHook( $e, $suppressed );
819 private static function callLogExceptionHook( Throwable $e,
bool $suppressed ) {
828 if ( !MediaWikiServices::hasInstance() || !class_exists( HookRunner::class,
false ) ) {
833 }
catch ( RecursiveServiceDependencyException $e ) {
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
wfIsCLI()
Check if we are running from the commandline.
WebRequest clone which takes values from a provided array.
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Handler class for MWExceptions.
static getLogContext(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Get a PSR-3 log event context from a Throwable.
static handleError( $level, $message, $file=null, $line=null)
Handler for set_error_handler() callback notifications.
static rollbackPrimaryChangesAndLog(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Roll back any open database transactions and log the stack trace of the throwable.
static installHandler(bool $logExceptionBacktrace=true, bool $propagateErrors=true)
Install handlers with PHP.
static getStructuredExceptionData(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Get a structured representation of a Throwable.
static rollbackMasterChangesAndLog(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
static getRedactedTraceAsString(Throwable $e)
Generate a string representation of a throwable's stack trace.
static report(Throwable $e)
Report a throwable to the user.
static logException(Throwable $e, $catcher=self::CAUGHT_BY_OTHER, $extraData=[])
Log a throwable to the exception log (if enabled).
static array $fatalErrorTypes
Error types that, if unhandled, are fatal to the request.
static getPublicLogMessage(Throwable $e)
static getRedactedTrace(Throwable $e)
Return a copy of a throwable's backtrace as an array.
static handleUncaughtException(Throwable $e)
Callback to use with PHP's set_exception_handler.
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
static string null $reservedMemory
static jsonSerializeException(Throwable $e, $pretty=false, $escaping=0, $catcher=self::CAUGHT_BY_OTHER)
Serialize a Throwable object to JSON.
static getLogMessage(Throwable $e)
Get a message formatting the throwable message and its origin.
static redactTrace(array $trace)
Redact a stacktrace generated by Throwable::getTrace(), debug_backtrace() or similar means.
static handleFatalError()
Callback used as a registered shutdown function.
static getLogNormalMessage(Throwable $e)
Get a normalised message for formatting with PSR-3 log event context.
static getURL()
If the exception occurred in the course of responding to a request, returns the requested URL.
static handleException(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Exception handler which simulates the appropriate catch() handling:
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.