Skip to content

ThrowableInfo

Mistralys edited this page Dec 19, 2019 · 4 revisions

This class is handy for debugging and keeping track of exceptions: it offers an easy, object oriented interface to access information on an exception. It features an improved trace, and the possibility to serialize the information for storage.

Method overview

Throwable info class

  • countCalls: the amount of calls in the stack trace
  • getCalls: retrieves all call instances in the stack trace
  • getCode: the error code
  • getContext: run context identifier: command line or web?
  • getDate: the date and approximate time the error occurred
  • getMessage: the error message text
  • getPrevious: if available, returns an info instance for the previous error
  • getReferer: URL of the page in which the error occurred
  • hasCode: whether the error specified an error code
  • hasPrevious: whether the error had a previous error specified
  • isCommandLine: whether PHP was running from the command line
  • isWebRequest: whether PHP was running from a webserver
  • serialize: serialize the information to an array
  • toString: get a text only stack trace

Stack trace call class

  • argumentsToString: convert all arguments to a text only string
  • getArguments: retrieve all arguments, as VariableInfo instances
  • getClass: the name of the class that was called, if any
  • getFileName: the path to the file of the call, if any
  • getFileRelative: relative path to the file (according to the folder depth option)
  • getFunction: the name of the function that was called, if any
  • getLine: the line number of the error
  • getPosition: 1-based index of the call in the stack
  • getType: identifier of the type of call (function call, method call?)
  • hasArguments: whether the call had any arguments
  • hasClass: whether this was a class method call
  • hasFunction: whether this was a function call
  • toString: get a text only string representation of the call

Basic usage examples

Use statements

To be able to work with the throwable info, you will need the following use statements:

use function AppUtils\parseThrowable;
use function AppUtils\restoreThrowable; // optional, see serialization.

Displaying an exception trace

try
{
    throw new Exception(
        'Error message',
        12345
    );
}
catch(Exception $e)
{
    // create an information instance from the exception
    $info = parseThrowable($e);

    // display the string trace
    echo $info;
}

The stack trace

To make it easier to work with the often confusing structure of the standard trace, the throwable info's stack trace is entirely object oriented, and IDE friendly.

This for example, retrieves a list of all classes called in the stack trace.

$info = parseThrowable($exception);

// retrieve all function calls
$calls = $info->getCalls();

$classNames = array();

foreach($calls as $call)
{
    if($call->hasClass()) 
    {
        $classNames[] = $call->getClass();
    }
}

Serialization

The throwable info instance can be serialized to an array, which can easily be stored - for example in a database, file, session, etc. It can then later be restored to a throwable info instance that has the same capabilities as the original.

Example for saving the error data to a file:

// create the data package
$data = parseThrowable($exception)->serialize();

// save the data to a file
FileHelper::saveAsJSON($data, 'error.json');

And restoring it later to review the data:

// load the data array from the file
$data = FileHelper::parseJSONFile('error.json');

// create the info instance from the data
$info = restoreThrowable($data);

New here?

Have a look at the overview for a list of all helper classes available in the package.

Table of contents

Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.

Clone this wiki locally