MediaWiki master
MWExceptionHandler Class Reference

Handler class for MWExceptions. More...

Static Public Member Functions

static getLogContext (Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
 Get a PSR-3 log event context from a Throwable.
 
static getLogMessage (Throwable $e)
 Get a message formatting the throwable message and its origin.
 
static getLogNormalMessage (Throwable $e)
 Get a normalised message for formatting with PSR-3 log event context.
 
static getPublicLogMessage (Throwable $e)
 
static getRedactedTrace (Throwable $e)
 Return a copy of a throwable's backtrace as an array.
 
static getRedactedTraceAsString (Throwable $e)
 Generate a string representation of a throwable's stack trace.
 
static getStructuredExceptionData (Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
 Get a structured representation of a Throwable.
 
static getURL ()
 If the exception occurred in the course of responding to a request, returns the requested URL.
 
static handleError ( $level, $message, $file=null, $line=null)
 Handler for set_error_handler() callback notifications.
 
static handleException (Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
 Exception handler which simulates the appropriate catch() handling:
 
static handleFatalError ()
 Callback used as a registered shutdown function.
 
static handleUncaughtException (Throwable $e)
 Callback to use with PHP's set_exception_handler.
 
static installHandler (bool $logExceptionBacktrace=true, bool $propagateErrors=true)
 Install handlers with PHP.
 
static jsonSerializeException (Throwable $e, $pretty=false, $escaping=0, $catcher=self::CAUGHT_BY_OTHER)
 Serialize a Throwable object to JSON.
 
static logException (Throwable $e, $catcher=self::CAUGHT_BY_OTHER, $extraData=[])
 Log a throwable to the exception log (if enabled).
 
static prettyPrintTrace (array $trace, $pad='')
 Generate a string representation of a stacktrace.
 
static redactTrace (array $trace)
 Redact a stacktrace generated by Throwable::getTrace(), debug_backtrace() or similar means.
 
static rollbackPrimaryChangesAndLog (Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
 Roll back any open database transactions and log the stack trace of the throwable.
 

Public Attributes

const CAUGHT_BY_ENTRYPOINT = 'entrypoint'
 Error caught and reported by a script entry point.
 
const CAUGHT_BY_HANDLER = 'mwe_handler'
 Error caught and reported by this exception handler.
 
const CAUGHT_BY_OTHER = 'other'
 Error reported by direct logException() call.
 

Static Protected Member Functions

static report (Throwable $e)
 Report a throwable to the user.
 

Static Protected Attributes

static string null $reservedMemory
 

Detailed Description

Handler class for MWExceptions.

Definition at line 36 of file MWExceptionHandler.php.

Member Function Documentation

◆ getLogContext()

static MWExceptionHandler::getLogContext ( Throwable $e,
$catcher = self::CAUGHT_BY_OTHER )
static

Get a PSR-3 log event context from a Throwable.

Creates a structured array containing information about the provided throwable that can be used to augment a log message sent to a PSR-3 logger.

Since
1.26
Parameters
Throwable$e
string$catcherCAUGHT_BY_* class constant indicating what caught the error
Returns
array

Definition at line 582 of file MWExceptionHandler.php.

◆ getLogMessage()

static MWExceptionHandler::getLogMessage ( Throwable $e)
static

Get a message formatting the throwable message and its origin.

Despite the method name, this is not used for logging. It is only used for HTML or CLI output by MWExceptionRenderer.

Since
1.22
Parameters
Throwable$e
Returns
string

Definition at line 513 of file MWExceptionHandler.php.

References $url.

◆ getLogNormalMessage()

static MWExceptionHandler::getLogNormalMessage ( Throwable $e)
static

Get a normalised message for formatting with PSR-3 log event context.

Must be used together with getLogContext() to be useful.

Since
1.30
Parameters
Throwable$e
Returns
string

Definition at line 541 of file MWExceptionHandler.php.

◆ getPublicLogMessage()

static MWExceptionHandler::getPublicLogMessage ( Throwable $e)
static
Parameters
Throwable$e
Returns
string

Definition at line 562 of file MWExceptionHandler.php.

◆ getRedactedTrace()

static MWExceptionHandler::getRedactedTrace ( Throwable $e)
static

Return a copy of a throwable's backtrace as an array.

Like Throwable::getTrace, but replaces each element in each frame's argument array with the name of its class (if the element is an object) or its type (if the element is a PHP primitive).

Since
1.22
Parameters
Throwable$e
Returns
array

Definition at line 466 of file MWExceptionHandler.php.

◆ getRedactedTraceAsString()

static MWExceptionHandler::getRedactedTraceAsString ( Throwable $e)
static

Generate a string representation of a throwable's stack trace.

Like Throwable::getTraceAsString, but replaces argument values with their type or class name, and prepends the start line of the throwable.

Parameters
Throwable$e
Returns
string
See also
prettyPrintTrace()

Definition at line 408 of file MWExceptionHandler.php.

◆ getStructuredExceptionData()

static MWExceptionHandler::getStructuredExceptionData ( Throwable $e,
$catcher = self::CAUGHT_BY_OTHER )
static

Get a structured representation of a Throwable.

Returns an array of structured data (class, message, code, file, backtrace) derived from the given throwable. The backtrace information will be redacted as per getRedactedTraceAsArray().

Parameters
Throwable$e
string$catcherCAUGHT_BY_* class constant indicating what caught the error
Returns
array
Since
1.26

Definition at line 613 of file MWExceptionHandler.php.

◆ getURL()

static MWExceptionHandler::getURL ( )
static

If the exception occurred in the course of responding to a request, returns the requested URL.

Otherwise, returns false.

Since
1.23
Returns
string|false

Definition at line 496 of file MWExceptionHandler.php.

References MW_ENTRY_POINT.

◆ handleError()

static MWExceptionHandler::handleError ( $level,
$message,
$file = null,
$line = null )
static

Handler for set_error_handler() callback notifications.

Receive a callback from the interpreter for a raised error, create an ErrorException, and log the exception to the 'error' logging channel(s).

Since
1.25
Parameters
int$levelError level raised
string$message
string | null$file
int | null$line
Returns
bool

Definition at line 254 of file MWExceptionHandler.php.

◆ handleException()

static MWExceptionHandler::handleException ( Throwable $e,
$catcher = self::CAUGHT_BY_OTHER )
static

Exception handler which simulates the appropriate catch() handling:

try { ... } catch ( Exception $e ) { $e->report(); } catch ( Exception $e ) { echo $e->__toString(); }

Since
1.25
Parameters
Throwable$e
string$catcherCAUGHT_BY_* class constant indicating what caught the error

Definition at line 235 of file MWExceptionHandler.php.

◆ handleFatalError()

static MWExceptionHandler::handleFatalError ( )
static

Callback used as a registered shutdown function.

This is used as callback from the interpreter at system shutdown. If the last error was not a recoverable error that we already reported, and log as fatal exception.

Special handling is included for missing class errors as they may indicate that the user needs to install 3rd-party libraries via Composer or other means.

Since
1.25
Returns
bool Always returns false

Definition at line 347 of file MWExceptionHandler.php.

◆ handleUncaughtException()

static MWExceptionHandler::handleUncaughtException ( Throwable $e)
static

Callback to use with PHP's set_exception_handler.

Since
1.31
Parameters
Throwable$e

Definition at line 204 of file MWExceptionHandler.php.

References wfIsCLI().

◆ installHandler()

static MWExceptionHandler::installHandler ( bool $logExceptionBacktrace = true,
bool $propagateErrors = true )
static

Install handlers with PHP.

Access: internal
Parameters
bool$logExceptionBacktraceWhether error handlers should include a backtrace in the log.
bool$propagateErrorsWhether errors should be propagated to PHP's built-in handler.

Definition at line 87 of file MWExceptionHandler.php.

◆ jsonSerializeException()

static MWExceptionHandler::jsonSerializeException ( Throwable $e,
$pretty = false,
$escaping = 0,
$catcher = self::CAUGHT_BY_OTHER )
static

Serialize a Throwable object to JSON.

The JSON object will have keys 'id', 'file', 'line', 'message', and 'url'. These keys map to string values, with the exception of 'line', which is a number, and 'url', which may be either a string URL or null if the throwable did not occur in the context of serving a web request.

If $wgLogExceptionBacktrace is true, it will also have a 'backtrace' key, mapped to the array return value of Throwable::getTrace, but with each element in each frame's "args" array (if set) replaced with the argument's class name (if the argument is an object) or type name (if the argument is a PHP primitive).

Sample JSON record ($wgLogExceptionBacktrace = false):
{
"id": "c41fb419",
"type": "Exception",
"file": "/var/www/mediawiki/includes/cache/MessageCache.php",
"line": 704,
"message": "Example message",
"url": "/wiki/Main_Page"
}
Sample JSON record ($wgLogExceptionBacktrace = true):
{
"id": "dc457938",
"type": "Exception",
"file": "/var/www/mediawiki/includes/cache/MessageCache.php",
"line": 704,
"message": "Example message",
"url": "/wiki/Main_Page",
"backtrace": [{
"file": "/var/www/mediawiki/includes/OutputPage.php",
"line": 80,
"function": "get",
"class": "MessageCache",
"type": "->",
"args": ["array"]
}]
}
Since
1.23
Parameters
Throwable$e
bool$prettyAdd non-significant whitespace to improve readability (default: false).
int$escapingBitfield consisting of FormatJson::.*_OK class constants.
string$catcherCAUGHT_BY_* class constant indicating what caught the error
Returns
string|false JSON string if successful; false upon failure

Definition at line 701 of file MWExceptionHandler.php.

◆ logException()

static MWExceptionHandler::logException ( Throwable $e,
$catcher = self::CAUGHT_BY_OTHER,
$extraData = [] )
static

Log a throwable to the exception log (if enabled).

This method must not assume the throwable is an MWException, it is also used to handle PHP exceptions or exceptions from other libraries.

Since
1.22
Parameters
Throwable$e
string$catcherCAUGHT_BY_* class constant indicating what caught the error
array$extraData(since 1.34) Additional data to log

Definition at line 725 of file MWExceptionHandler.php.

◆ prettyPrintTrace()

static MWExceptionHandler::prettyPrintTrace ( array $trace,
$pad = '' )
static

Generate a string representation of a stacktrace.

Since
1.26
Parameters
array$trace
string$padConstant padding to add to each line of trace
Returns
string

Definition at line 421 of file MWExceptionHandler.php.

◆ redactTrace()

static MWExceptionHandler::redactTrace ( array $trace)
static

Redact a stacktrace generated by Throwable::getTrace(), debug_backtrace() or similar means.

Replaces each element in each frame's argument array with the name of its class (if the element is an object) or its type (if the element is a PHP primitive).

Since
1.26
Parameters
array$traceStacktrace
Returns
array Stacktrace with argument values converted to data types

Definition at line 480 of file MWExceptionHandler.php.

◆ report()

static MWExceptionHandler::report ( Throwable $e)
staticprotected

Report a throwable to the user.

Parameters
Throwable$e

Definition at line 124 of file MWExceptionHandler.php.

◆ rollbackPrimaryChangesAndLog()

static MWExceptionHandler::rollbackPrimaryChangesAndLog ( Throwable $e,
$catcher = self::CAUGHT_BY_OTHER )
static

Roll back any open database transactions and log the stack trace of the throwable.

This method is used to attempt to recover from exceptions

Since
1.37
Parameters
Throwable$e
string$catcherCAUGHT_BY_* class constant indicating what caught the error

Definition at line 189 of file MWExceptionHandler.php.

Member Data Documentation

◆ $reservedMemory

string null MWExceptionHandler::$reservedMemory
staticprotected

Definition at line 45 of file MWExceptionHandler.php.

◆ CAUGHT_BY_ENTRYPOINT

const MWExceptionHandler::CAUGHT_BY_ENTRYPOINT = 'entrypoint'

Error caught and reported by a script entry point.

Definition at line 40 of file MWExceptionHandler.php.

◆ CAUGHT_BY_HANDLER

const MWExceptionHandler::CAUGHT_BY_HANDLER = 'mwe_handler'

Error caught and reported by this exception handler.

Definition at line 38 of file MWExceptionHandler.php.

◆ CAUGHT_BY_OTHER

const MWExceptionHandler::CAUGHT_BY_OTHER = 'other'

Error reported by direct logException() call.

Definition at line 42 of file MWExceptionHandler.php.


The documentation for this class was generated from the following file: