MediaWiki REL1_33
MWExceptionHandler Class Reference

Handler class for MWExceptions. More...

Static Public Member Functions

static getLogContext ( $e, $catcher=self::CAUGHT_BY_OTHER)
 Get a PSR-3 log event context from an Exception.
 
static getLogMessage ( $e)
 Get a message formatting the exception message and its origin.
 
static getLogNormalMessage ( $e)
 Get a normalised message for formatting with PSR-3 log event context.
 
static getPublicLogMessage ( $e)
 
static getRedactedTrace ( $e)
 Return a copy of an exception's backtrace as an array.
 
static getRedactedTraceAsString ( $e)
 Generate a string representation of an exception's stack trace.
 
static getStructuredExceptionData ( $e, $catcher=self::CAUGHT_BY_OTHER)
 Get a structured representation of an Exception.
 
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 ( $e)
 Exception handler which simulates the appropriate catch() handling:
 
static handleFatalError ( $level=null, $message=null, $file=null, $line=null, $context=null, $trace=null)
 Dual purpose callback used as both a set_error_handler() callback and a registered shutdown function.
 
static handleUncaughtException ( $e)
 Callback to use with PHP's set_exception_handler.
 
static installHandler ()
 Install handlers with PHP.
 
static jsonSerializeException ( $e, $pretty=false, $escaping=0, $catcher=self::CAUGHT_BY_OTHER)
 Serialize an Exception object to JSON.
 
static logException ( $e, $catcher=self::CAUGHT_BY_OTHER)
 Log an exception 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 Exception::getTrace(), debug_backtrace() or similar means.
 
static rollbackMasterChangesAndLog ( $e)
 Roll back any open database transactions and log the stack trace of the exception.
 

Public Attributes

const CAUGHT_BY_HANDLER = 'mwe_handler'
 
const CAUGHT_BY_OTHER = 'other'
 

Static Protected Member Functions

static logError (ErrorException $e, $channel, $level=LogLevel::ERROR)
 Log an exception that wasn't thrown but made to wrap an error.
 
static report ( $e)
 Report an exception to the user.
 

Static Protected Attributes

static $fatalErrorTypes
 Error types that, if unhandled, are fatal to the request.
 
static $handledFatalCallback = false
 
static $reservedMemory
 

Detailed Description

Handler class for MWExceptions.

Definition at line 30 of file MWExceptionHandler.php.

Member Function Documentation

◆ getLogContext()

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

Get a PSR-3 log event context from an Exception.

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

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

Definition at line 539 of file MWExceptionHandler.php.

References $e, and and().

◆ getLogMessage()

static MWExceptionHandler::getLogMessage ( $e)
static

Get a message formatting the exception message and its origin.

Since
1.22
Parameters
Exception | Throwable$e
Returns
string

Definition at line 487 of file MWExceptionHandler.php.

References $e, $line, $type, and and().

◆ getLogNormalMessage()

static MWExceptionHandler::getLogNormalMessage ( $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
Exception | Throwable$e
Returns
string

Definition at line 507 of file MWExceptionHandler.php.

References $e, $line, $type, and and().

◆ getPublicLogMessage()

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

Definition at line 520 of file MWExceptionHandler.php.

References $e, $type, and and().

◆ getRedactedTrace()

static MWExceptionHandler::getRedactedTrace ( $e)
static

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

Like Exception::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
Exception | Throwable$e
Returns
array

Definition at line 440 of file MWExceptionHandler.php.

References $e.

Referenced by MediaWiki\Logger\LegacyLogger\format().

◆ getRedactedTraceAsString()

static MWExceptionHandler::getRedactedTraceAsString ( $e)
static

Generate a string representation of an exception's stack trace.

Like Exception::getTraceAsString, but replaces argument values with argument type or class name.

Parameters
Exception | Throwable$e
Returns
string
See also
prettyPrintTrace()

Definition at line 381 of file MWExceptionHandler.php.

References $e.

◆ getStructuredExceptionData()

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

Get a structured representation of an Exception.

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

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

Definition at line 560 of file MWExceptionHandler.php.

References $data, $e, $wgLogExceptionBacktrace, and and().

◆ 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 472 of file MWExceptionHandler.php.

References $wgRequest, and and().

◆ 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). If the raised error is a fatal error type (only under HHVM) delegate to handleFatalError() instead.

Since
1.25
Parameters
int$levelError level raised
string$message
string | null$file
int | null$line
Returns
bool
See also
logError()

Definition at line 193 of file MWExceptionHandler.php.

References $e, $line, $wgPropagateErrors, and and().

◆ handleException()

static MWExceptionHandler::handleException ( $e)
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
Exception | Throwable$e

Definition at line 170 of file MWExceptionHandler.php.

References $e.

◆ handleFatalError()

static MWExceptionHandler::handleFatalError ( $level = null,
$message = null,
$file = null,
$line = null,
$context = null,
$trace = null )
static

Dual purpose callback used as both a set_error_handler() callback and a registered shutdown function.

Receive a callback from the interpreter for a raised error or system shutdown, check for a fatal error, and log to the 'fatal' logging channel.

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
Parameters
int | null$levelError level raised
string | null$messageError message
string | null$fileFile that error was raised in
int | null$lineLine number error was raised at
array | null$contextActive symbol table point of error
array | null$traceBacktrace at point of error (undocumented HHVM feature)
Returns
bool Always returns false

Definition at line 283 of file MWExceptionHandler.php.

References $file, $line, and(), and href.

◆ handleUncaughtException()

static MWExceptionHandler::handleUncaughtException ( $e)
static

Callback to use with PHP's set_exception_handler.

Since
1.31
Parameters
Exception | Throwable$e

Definition at line 143 of file MWExceptionHandler.php.

References $e, and(), and wfIsCLI().

◆ installHandler()

static MWExceptionHandler::installHandler ( )
static

Install handlers with PHP.

Definition at line 76 of file MWExceptionHandler.php.

References and().

◆ jsonSerializeException()

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

Serialize an Exception 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 or null if the exception 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 Exception::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": "MWException",
"file": "/var/www/mediawiki/includes/cache/MessageCache.php",
"line": 704,
"message": "Non-string key given",
"url": "/wiki/Main_Page"
}
Sample JSON record ($wgLogExceptionBacktrace = true):
{
"id": "dc457938",
"type": "MWException",
"file": "/vagrant/mediawiki/includes/cache/MessageCache.php",
"line": 704,
"message": "Non-string key given",
"url": "/wiki/Main_Page",
"backtrace": [{
"file": "/vagrant/mediawiki/extensions/VisualEditor/VisualEditor.hooks.php",
"line": 80,
"function": "get",
"class": "MessageCache",
"type": "->",
"args": ["array"]
}]
}
Since
1.23
Parameters
Exception | 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 647 of file MWExceptionHandler.php.

References $e.

◆ logError()

static MWExceptionHandler::logError ( ErrorException $e,
$channel,
$level = LogLevel::ERROR )
staticprotected

Log an exception that wasn't thrown but made to wrap an error.

Since
1.25
Parameters
ErrorException$e
string$channel
string$level

Definition at line 693 of file MWExceptionHandler.php.

References $e, $suppressed, and and().

◆ logException()

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

Log an exception to the exception log (if enabled).

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

Since
1.22
Parameters
Exception | Throwable$e
string$catcherCAUGHT_BY_* class constant indicating what caught the error

Definition at line 667 of file MWExceptionHandler.php.

References $e.

◆ prettyPrintTrace()

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

Generate a string representation of a stacktrace.

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

Definition at line 393 of file MWExceptionHandler.php.

References and().

Referenced by MediaWiki\Logger\LegacyLogger\format(), and MediaWiki\Logger\Monolog\LineFormatter\normalizeExceptionArray().

◆ redactTrace()

static MWExceptionHandler::redactTrace ( array $trace)
static

Redact a stacktrace generated by Exception::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 arugment values converted to data types

Definition at line 454 of file MWExceptionHandler.php.

References and().

Referenced by MediaWiki\Logger\Monolog\LineFormatter\exceptionAsArray().

◆ report()

static MWExceptionHandler::report ( $e)
staticprotected

Report an exception to the user.

Parameters
Exception | Throwable$e

Definition at line 89 of file MWExceptionHandler.php.

References $e, MWExceptionRenderer\AS_PRETTY, MWExceptionRenderer\AS_RAW, and MWExceptionRenderer\output().

◆ rollbackMasterChangesAndLog()

static MWExceptionHandler::rollbackMasterChangesAndLog ( $e)
static

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

This method is used to attempt to recover from exceptions

Since
1.23
Parameters
Exception | Throwable$e

Definition at line 116 of file MWExceptionHandler.php.

References $e, and $services.

Member Data Documentation

◆ $fatalErrorTypes

array MWExceptionHandler::$fatalErrorTypes
staticprotected
Initial value:
= [
16777217,
]
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy

Error types that, if unhandled, are fatal to the request.

On PHP 7, these error types may be thrown as Error objects, which implement Throwable (but not Exception).

On HHVM, these invoke the set_error_handler callback, similar to how (non-fatal) warnings and notices are reported, except that after this handler runs for fatal error tpyes, script execution stops!

The user will be shown an HTTP 500 Internal Server Error. As such, these should be sent to MediaWiki's "fatal" or "exception" channel. Normally, the error handler logs them to the "error" channel.

Definition at line 55 of file MWExceptionHandler.php.

◆ $handledFatalCallback

bool MWExceptionHandler::$handledFatalCallback = false
staticprotected

Definition at line 71 of file MWExceptionHandler.php.

◆ $reservedMemory

string MWExceptionHandler::$reservedMemory
staticprotected

Definition at line 37 of file MWExceptionHandler.php.

◆ CAUGHT_BY_HANDLER

const MWExceptionHandler::CAUGHT_BY_HANDLER = 'mwe_handler'

Definition at line 31 of file MWExceptionHandler.php.

◆ CAUGHT_BY_OTHER

const MWExceptionHandler::CAUGHT_BY_OTHER = 'other'

Definition at line 32 of file MWExceptionHandler.php.


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