Source for file ExceptionDump.php
Documentation is available at ExceptionDump.php
require_once 'Gtk2/ExceptionDump/InfoBox.php';
require_once 'Gtk2/ExceptionDump/Stack.php';
* Displays an Exception in a GtkWindow.
* Supports PHP Exceptions and PEAR_Error objects.
* $error = new PEAR_Error();
* Gtk2_ExceptionDump::display($error);
* Gtk2_ExceptionDump::setupExceptionHandler();
* throw new Exception('Exception!');
* Gtk2_ExceptionDump::setupPearErrorHandler();
* PEAR::raiserError('Error!');
* Gtk2_ExceptionDump::setupPhpErrorHandler();
* trigger_error('Oops! A php error', E_USER_ERROR);
* @package Gtk2_ExceptionDump
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear.php.net/package/Gtk2_ExceptionDump
* The exception that is shown.
* PEAR_Error or Exception.
* Creates a new ExceptionDump window.
* You still need to show it and run the main loop.
* The window can be closed, quitting the main loop and
* continuing running the program OR quitting the script
* @param mixed $exception Exception or PEAR_Error object
* @param string $title The title for the window
if ($exception !== null ) {
}//public function __construct($exception, $title = null)
* Creates a new ExceptionDump window, displays it
* and starts its own main loop.
* The window can be closed, quitting the main loop and
* continuing running the program OR quitting the script
* @param mixed $exception Exception or PEAR_Error object
* @param string $title (optional) The title for the window
public static function display($exception, $title = null )
}//public static function display($exception = null, $title = null)
* Sets up the PHP exception handler to call
* Gtk2_ExceptionDump::display() if an exception occurs.
* This is the safest way to handle *all* uncaught exceptions
* with Gtk2_ExceptionDump. It handles "real" exceptions only,
* Note that the "continue" button doesn't work anymore, since
* the exceptions are handled out of all user php code.
}//public static function setupExceptionHandler()
* Sets up the PEAR Exception handler to call
* Gtk2_ExceptionDump::display() if an PEAR_Error occurs.
* While this catches *all* PEAR_Errors, it also catches
* the ones that are handled by php scripts.
PEAR ::setErrorHandling (PEAR_ERROR_CALLBACK ,
array ('Gtk2_ExceptionDump', 'display'));
}//public static function setupPearErrorHandler()
* Sets the php error handler to use Gtk2_ExceptionDump.
* All catchable PHP errors are displayed here.
* Not all errors are handled, only the ones
* defined by error_reporting.
}//public static function setupPhpErrorHandler()
* Sets up that all errors/exceptions are handled with
* Calls setupExceptionHandler() and setupPearErrorHandler()
self ::setupExceptionHandler ();
self ::setupPearErrorHandler ();
self ::setupPhpErrorHandler ();
}//public static function setupAllHandlers()
* Use Gtk2_ExceptionDump to display a PHP error.
* @param int $errno Contains the level of the error raised
* @param string $errstr Error message
* @param string $errfile Filename that the error was raised in
* @param int $errline Line number the error was raised at
* @param array $errcontext Array that points to the active symbol
* table at the point the error occurred. In
* other words, errcontext will contain an
* array of every variable that existed in
* the scope the error was triggered in.
public static function handlePhpError($errno, $errstr, $errfile = null ,
$errline = null , $errcontext = array ())
//this occurs when using @ to silence errors
E_WARNING => 'E_WARNING',
E_CORE_ERROR => 'E_CORE_ERROR',
E_CORE_WARNING => 'E_CORE_WARNING',
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
E_USER_ERROR => 'E_USER_ERROR',
E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE',
new Exception ($errstr . "\r\n\r\n" . $errfile . '#' . $errline, $errno),
'PHP Error: ' . $errorNames[$errno]
}//public static function handlePhpError($errno, $errstr, $errfile = null,
// $errline = null , $errcontext = array())
* Set the exception object.
* @param PEAR_Error|Exception$exception Exception to display
* @param mixed $title Window title to use
if ($exception instanceof PEAR_Error || $exception instanceof Exception ) {
$this->stack->setException ($exception);
$this->infobox->setException ($exception);
$this->set_title ('No error occured');
}//public function setException($exception, $title = null)
* Sets the title of the window.
* @param PEAR_Error|Exception$exception Exception to display
* @param string $title Window title to use
protected function setTitle($exception, $title)
if ($exception instanceof PEAR_Error ) {
if ($class == 'PEAR_Error') {
$title = 'A PEAR_Error was thrown';
$title = 'A "' . $class . '" PEAR_Error was thrown';
if ($class == 'Exception') {
$title = 'An Exception was thrown' ;
$title = 'An "' . $class . '" exception was thrown' ;
$this->set_title ($title);
}//protected function setTitle($exception, $title)
* Creates the dialog widgets.
= $this->connect_simple ('destroy', array ($this, 'onQuit'));
$this->set_default_size (700 , 300 );
$scrStack = new GtkScrolledWindow ();
$scrStack->set_policy (Gtk ::POLICY_AUTOMATIC , Gtk ::POLICY_AUTOMATIC );
$scrStack->add ($this->stack);
$vbox->pack_start ($this->infobox, false );
$vbox->pack_start ($scrStack);
$vbox->pack_end ($hbox, false );
}//protected function buildDialog($title)
* Returns the box with the Execute/Continue buttons.
* @return GtkButtonBox The button box.
$buttons = new GtkHButtonBox ();
$buttons->set_layout (Gtk ::BUTTONBOX_END );
$quit = GtkButton ::new_from_stock (Gtk ::STOCK_QUIT );
$quit->connect_simple ('clicked', array ($this, 'onQuit'));
$quit->set_flags ($quit->flags () + Gtk ::CAN_DEFAULT );
//FIXME: register own stock icon
$continue = GtkButton ::new_from_stock (Gtk ::STOCK_EXECUTE );
$continue->set_label ('Con_tinue');
$continue->connect_simple ('clicked', array ($this, 'onContinue'));
$buttons->add ($continue);
}//protected function buildContinuationButtonBox()
* Returns the box with the Copy button.
* @return GtkButtonBox The button box.
$buttons = new GtkHButtonBox ();
$buttons->set_layout (Gtk ::BUTTONBOX_END );
$copy = GtkButton ::new_from_stock (Gtk ::STOCK_COPY );
$copy->connect_simple ('clicked', array ($this, 'onCopy'));
}//protected function buildActionButtonBox()
* Creates the error message that the object isn't an
* Exception or a PEAR_Error.
* @param mixed $exception Some variable that isn't an Exception
$this->infobox->setMessage (
'Gtk2_ExceptionDump has been called with'
. ' an variable of type "' . gettype($exception) . '".' . "\n"
. 'It cannot be displayed since it is not'
. ' an Exception or an PEAR_Error object.',
'Usually, this occurs when the programmer didn\'t check' . "\n"
. 'what type of variable is passed to Gtk2_ExceptionDump.' . "\n"
$this->stack->setException (new Exception (), 3 );
}//protected function buildError($exception)
* Quits the php script with exit status 253.
}//public function onQuit()
* Closes the window, quits the main loop
* and continues normal script execution.
//block the destroy signal, since it would call onQuit()
$this->block ($this->destroySignal);
}//public function onContinue()
* Copies the exception/error as string to the clipboard.
$cb = GtkClipboard ::get (Gdk ::atom_intern ('CLIPBOARD', false ));
$cb->set_text (self ::getExceptionAsString ($this->exception));
}//public function onCopy()
* Generates a string representation of the exception.
* @param mixed $exception The exception to convert
' ' . $exception->getMessage ()
if ($exception instanceof PEAR_Error ) {
$s[] = ' ' . $exception->getUserInfo ();
$func = $exception instanceof PEAR_Error ? 'getBacktrace' : 'getTrace';
$trace = $exception->$func();
foreach ($trace as $id => $step) {
if (count($step['args']) == 0 ) {
$function = $step['function'] . '()';
$function = $step['function'] . '(...)';
if (isset ($step['class'])) {
if ($step['class'] == $step['function']) {
$function = 'new ' . $function;
$function = $step['class'] . $step['type'] . $function;
if (isset ($step['file'])) {
$file = $step['file'] . '#' . $step['line'];
$s[] = ' ' . $function. ' at ' . $file;
foreach ($step['args'] as $arg) {
$arg = get_class($arg) . ' - ' . (string) $arg;
$arg = gettype($arg) . '(' . $arg . ')';
}//protected static function getExceptionAsString($exception)
}//class Gtk2_ExceptionDump extends GtkWindow
Documentation generated on Mon, 11 Mar 2019 15:55:05 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|