MediaWiki  1.23.2
MWExceptionHandler Class Reference

Handler class for MWExceptions. More...

Static Public Member Functions

static getLogId (Exception $e)
 Get the ID for this error. More...
 
static getLogMessage (Exception $e)
 Return the requested URL and point to file and line number from which the exception occurred. More...
 
static getRedactedTrace (Exception $e)
 Return a copy of an exception's backtrace as an array. More...
 
static getRedactedTraceAsString (Exception $e)
 Generate a string representation of an exception's stack trace. More...
 
static getURL ()
 If the exception occurred in the course of responding to a request, returns the requested URL. More...
 
static handle ( $e)
 Exception handler which simulates the appropriate catch() handling: More...
 
static installHandler ()
 Install an exception handler for MediaWiki exception types. More...
 
static jsonSerializeException (Exception $e, $pretty=false, $escaping=0)
 Serialize an Exception object to JSON. More...
 
static logException (Exception $e)
 Log an exception to the exception log (if enabled). More...
 
static printError ( $message)
 Print a message, if possible to STDERR. More...
 
static rollbackMasterChangesAndLog (Exception $e)
 If there are any open database transactions, roll them back and log the stack trace of the exception that should have been caught so the transaction could be aborted properly. More...
 

Static Protected Member Functions

static report (Exception $e)
 Report an exception to the user. More...
 

Detailed Description

Handler class for MWExceptions.

Definition at line 25 of file MWExceptionHandler.php.

Member Function Documentation

◆ getLogId()

static MWExceptionHandler::getLogId ( Exception  $e)
static

Get the ID for this error.

The ID is saved so that one can match the one output to the user (when $wgShowExceptionDetails is set to false), to the entry in the debug log.

Since
1.22
Parameters
Exception$e
Returns
string

Definition at line 227 of file MWExceptionHandler.php.

References $e, and wfRandomString().

Referenced by MWException\getHTML(), MWException\getLogId(), and getLogMessage().

◆ getLogMessage()

static MWExceptionHandler::getLogMessage ( Exception  $e)
static

Return the requested URL and point to file and line number from which the exception occurred.

Since
1.22
Parameters
Exception$e
Returns
string

Definition at line 257 of file MWExceptionHandler.php.

References $e, $file, $line, getLogId(), and getURL().

Referenced by MWException\getHTML(), MWException\getLogMessage(), MWException\getText(), logException(), report(), and rollbackMasterChangesAndLog().

◆ getRedactedTrace()

static MWExceptionHandler::getRedactedTrace ( Exception  $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$e
Returns
array

Definition at line 206 of file MWExceptionHandler.php.

References $e.

Referenced by jsonSerializeException(), and MWExceptionHandlerTest\testGetRedactedTrace().

◆ getRedactedTraceAsString()

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

Definition at line 163 of file MWExceptionHandler.php.

References $e, and as.

Referenced by MWException\getHTML(), MWException\getText(), and report().

◆ 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|bool

Definition at line 241 of file MWExceptionHandler.php.

References global.

Referenced by getLogMessage(), and jsonSerializeException().

◆ handle()

static MWExceptionHandler::handle (   $e)
static

Exception handler which simulates the appropriate catch() handling:

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

Definition at line 134 of file MWExceptionHandler.php.

References $e, $wgFullyInitialised, global, report(), rollbackMasterChangesAndLog(), and wfLogProfilingData().

Referenced by MediaWiki\run().

◆ installHandler()

static MWExceptionHandler::installHandler ( )
static

Install an exception handler for MediaWiki exception types.

Definition at line 29 of file MWExceptionHandler.php.

References array().

◆ jsonSerializeException()

static MWExceptionHandler::jsonSerializeException ( Exception  $e,
  $pretty = false,
  $escaping = 0 
)
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",
"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",
"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$e
bool$prettyAdd non-significant whitespace to improve readability (default: false).
int$escapingBitfield consisting of FormatJson::.*_OK class constants.
Returns
string|bool: JSON string if successful; false upon failure

Definition at line 318 of file MWExceptionHandler.php.

References $e, array(), FormatJson\encode(), getRedactedTrace(), getURL(), and global.

Referenced by logException(), MWExceptionTest\testJsonserializeexceptionBacktracingDisabled(), MWExceptionTest\testJsonserializeexceptionBacktracingEnabled(), MWExceptionTest\testJsonserializeexceptionKeys(), and MWExceptionTest\testJsonSerializeExceptions().

◆ logException()

◆ printError()

static MWExceptionHandler::printError (   $message)
static

Print a message, if possible to STDERR.

Use this in command line mode only (see isCommandLine)

Parameters
string$messageFailure text

Definition at line 93 of file MWExceptionHandler.php.

Referenced by report(), and MWException\report().

◆ report()

static MWExceptionHandler::report ( Exception  $e)
staticprotected

Report an exception to the user.

Definition at line 36 of file MWExceptionHandler.php.

References $e, getLogMessage(), getRedactedTraceAsString(), global, MWException\isCommandLine(), and printError().

Referenced by handle().

◆ rollbackMasterChangesAndLog()

static MWExceptionHandler::rollbackMasterChangesAndLog ( Exception  $e)
static

If there are any open database transactions, roll them back and log the stack trace of the exception that should have been caught so the transaction could be aborted properly.

Since
1.23
Parameters
Exception$e

Definition at line 111 of file MWExceptionHandler.php.

References $e, $factory, getLogMessage(), wfDebugLog(), and wfGetLBFactory().

Referenced by RunJobs\execute(), SpecialRunJobs\executeJobs(), handle(), ApiMain\handleException(), AssembleUploadChunksJob\run(), and PublishStashedFileJob\run().


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