Like this code?
Show the author your appreciation.
This code will take some hacking to get it to fit your needs (probably) as i have ripped it out of one of the projects i did. Please keep in mind this code will not work out of the box, but it will give you a nice idea of how to implement it. This code should take very little work to get it working for you, as you will see the stack trace will be neatly formatted where you can click a + to expand and see the details of each error in the trace. One thing i ripped out of this code was a check to see if the site was being run on a local dev server, if it is then it would show the real errors and the trace, if not it would show a generic error because we didnt want our end users (clients) to see any of the error information.
Enjoy and please leave constructive criticism
I should also mention this code was written some time ago so there may be sloppyness ;)
There are 2 classed in use here SvEx and Trace
SvEx
<?php
/**
* The SvEx class
*
* This is the exception class for the main Application
*
* @author Joseph Crawford Jr. <codebowl@gmail.com>
* @copyright Codebowl Solutions 2005
* @package SimonVolkov
* @subpackage Exceptions
*
**/
class SvEx extends Exception {
/**#@+
* @var constant
**/
const ERR_HEAD = "An Error Occured..." ;
const ERR_TYPE = "Unknown Error" ;
const ERR_DESC = "There has been an error. This error has been logged. Please try again in a few minutes." ;
/**#@+
* @access private
* @var integer
**/
private $_line ;
/**#@+
* @access private
* @var string
**/
private $_file ;
/**#@+
* @access protected
* @var string
**/
protected $_msg ;
/**#@+
* @access protected
* @var object
**/
protected $_tpl ;
/**#@+
* @access protected
* @var mixed
**/
protected $_error ;
/**
*
* Constructor
*
* This sets everything up for the SvEx Exception object.
* This will create the template object and set the error type
* and description to the default exception values. This also
* sets the error message and calls the parent Built In Exception's
* constructor.
*
* @access public
* @param string $error
* @return void
*
**/
public function __construct ( $error = null , $file = null , $line = null ) {
if( ! is_null ( $file ) ) $this -> _file = $file ;
if( ! is_null ( $line ) ) $this -> _line = $line ;
$this -> _tpl = Application :: Template ();
$this -> Type ( self :: ERR_TYPE );
$this -> Desc ( self :: ERR_DESC );
$this -> _msg = $error ;
parent :: __construct ( $this -> _msg );
}
/**
*
* Display Error Page
*
* This method set's all the template stuff so that the nicely
* formatted error page can be displayed.
*
* @access public
* @param string $file
* @param integer $line
* @return void
*
**/
public function Display ( $trace , $msg = "" ) {
$this -> _tpl -> assign ( 'error_head' , self :: ERR_HEAD );
$this -> _tpl -> assign ( 'error_type' , $this -> _error [ 'type' ] );
$this -> _tpl -> assign ( 'error_description' , $this -> _error [ 'description' ] );
$this -> _tpl -> assign ( 'show_trace' , true );
$this -> _tpl -> assign ( 'trace' , $this -> BuildTrace ( $trace ) );
if( $msg != "" ) $this -> _tpl -> assign ( 'msg' , $msg );
$this -> _tpl -> display ( 'error.tpl' );
}
protected function BuildTrace ( $trace ) {
$this -> _tpl -> assign ( 'trace' , 1 );
$nTrace = new Trace ( $trace );
return $nTrace -> Build ();
}
}
?>
Trace
<?php
class Trace {
private $_trace ;
private $_string ;
public function __construct ( $trace ) {
$this -> _trace = $trace ;
$this -> _string = '' ;
$this -> _c [ 'default' ] = '#000000' ;
$this -> _c [ 'keyword' ] = '#0000A0' ;
$this -> _c [ 'number' ] = '#800080' ;
$this -> _c [ 'string' ] = '#404040' ;
$this -> _c [ 'comment' ] = '#808080' ;
}
public function Build () {
if ( count ( $this -> _trace ) ) {
$this -> _string .= '<span style="font-family: monospaced; font-size: 11px;">Trace: ' . count ( $this -> _trace ) . "</span> " ;
$this -> _string .= '<span style="font-family: monospaced; font-size: 11px; cursor: pointer;" onclick="showDetails(' . count ( $this -> _trace ). ')">[show details]</span> ' ;
$this -> _string .= '<span style="font-family: monospaced; font-size: 11px; cursor: pointer;" onclick="hideDetails(' . count ( $this -> _trace ). ')">[hide details]</span>' ;
$this -> _string .= "<br />" ;
$this -> _string .= "<br />" ;
$this -> _string .= '<ul>' ;
$this -> _currentParam = - 1 ;
foreach ( $this -> _trace as $k => $v ) {
$this -> _currentParam ++;
$this -> _string .= '<li style="list-style-type: square;">' ;
if (isset( $v [ 'class' ])) {
$this -> _string .= '<span onmouseover="this.style.color=\'#0000ff\'" onmouseout="this.style.color=\'' . $this -> _c [ 'keyword' ]. '\'" style="color: ' . $this -> _c [ 'keyword' ]. '; cursor: pointer;" onclick="showFile(' . $k . ')">' ;
$this -> _string .= $v [ 'class' ];
$this -> _string .= "->" ;
} else {
$this -> _string .= '<span onmouseover="this.style.color=\'#0000ff\'" onmouseout="this.style.color=\'' . $this -> _c [ 'keyword' ]. '\'" style="color: ' . $this -> _c [ 'keyword' ]. '; cursor: pointer;" onclick="showFile(' . $k . ')">' ;
}
$this -> _string .= $v [ 'function' ];
$this -> _string .= '</span>' ;
$this -> _string .= " (" ;
$sep = '' ;
$v [ 'args' ] = (array) @ $v [ 'args' ];
foreach ( $v [ 'args' ] as $arg ) {
$this -> _currentParam ++;
$this -> _string .= $sep ;
$sep = ', ' ;
$this -> _color = '#404040' ;
switch ( true ) {
case is_bool ( $arg ):
$param = 'TRUE' ;
$string = $param ;
break;
case is_int ( $arg ):
case is_float ( $arg ):
$param = $arg ;
$string = $arg ;
$this -> _color = $this -> _c [ 'number' ];
break;
case is_null ( $arg ):
$param = 'NULL' ;
$string = $param ;
break;
case is_string ( $arg ):
$param = $arg ;
$string = 'string[' . strlen ( $arg ) . ']' ;
break;
case is_array ( $arg ):
ob_start ();
print_r ( $arg );
$param = ob_get_contents ();
ob_end_clean ();
$string = 'array[' . count ( $arg ) . ']' ;
break;
case is_object ( $arg ):
ob_start ();
print_r ( $arg );
$param = ob_get_contents ();
ob_end_clean ();
$string = 'object: ' . get_class ( $arg );
break;
case is_resource ( $arg ):
$param = 'resource: ' . get_resource_type ( $arg );
$string = 'resource' ;
break;
default:
$param = 'unknown' ;
$string = $param ;
break;
}
$this -> _string .= '<span style="cursor: pointer; color: ' . $this -> _color . ';" onclick="showOrHideParam(' . $this -> _currentParam . ')" onmouseout="this.style.color=\'' . $this -> _color . '\'" onmouseover="this.style.color=\'#dd0000\'">' ;
$this -> _string .= $string ;
$this -> _string .= '</span>' ;
$this -> _string .= '<span id="param' . $this -> _currentParam . '" style="display: none;">' . $param . '</span>' ;
}
$this -> _string .= ")" ;
$this -> _string .= "<br />" ;
if (!isset( $v [ 'file' ])) {
$v [ 'file' ] = 'unknown' ;
}
if (!isset( $v [ 'line' ])) {
$v [ 'line' ] = 'unknown' ;
}
$v [ 'line' ] = @ $v [ 'line' ];
$this -> _string .= '<span id="file' . $k . '" style="display: none; color: gray;">' ;
if ( $v [ 'file' ] && $v [ 'line' ]) {
$this -> _string .= 'FILE: ' . basename ( $v [ 'file' ]);
} else {
$this -> _string .= 'FILE: ' . $this -> fontStart ( '#007700' ) . basename ( $v [ 'file' ]) . $this -> fontEnd ();
}
$this -> _string .= "<br />" ;
$this -> _string .= 'LINE: ' . $this -> fontStart ( '#007700' ) . $v [ 'line' ] . $this -> fontEnd () . "<br />" ;
$this -> _string .= 'DIR: ' . $this -> fontStart ( '#007700' ) . dirname ( $v [ 'file' ]) . $this -> fontEnd ();
$this -> _string .= '</span>' ;
$this -> _string .= '</li>' ;
}
$this -> _string .= '</ul>' ;
} else {
$this -> _string .= '<b>File:</b> ' ;
$this -> _string .= basename ( $file );
$this -> _string .= ' (' . $line . ') ' ;
$this -> _string .= dirname ( $file );
}
$this -> _string .= '<span id="paramHide" style="display: none; font-family: monospaced; font-size: 11px; cursor: pointer;" onclick="hideParam()">[hide param]</span>' ;
$this -> _string .= '<span id="paramSpace" style="display: none;"></span>' ;
$this -> _string .= '<div id="param" perm="0" style=" width: 606px; background-color: #FFFFE1; padding: 2px; display: none;"></div>' ;
return $this -> _string ;
}
private function fontStart ( $color ) {
return '<font color="' . $color . '">' ;
}
private function fontEnd () {
return '</font>' ;
}
}
?>
DISPLAY Trace FILE
<head>
{literal}
<style type="text/css">
<!--
.style66 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style67 {
color: #CC0000;
font-weight: bold;
}
-->
</style>
{/literal}
{if $trace }
{literal}
<script type="text/javascript">
function showParam(i) {
currentParam = i;
document.getElementById('paramHide').style.display = ''
document.getElementById('paramSpace').style.display = ''
document.getElementById('param').style.display = ''
document.getElementById('param').innerHTML = '<code>' + document.getElementById('param' + i).innerHTML + '</code>'
}
function hideParam() {
currentParam = -1;
document.getElementById('paramHide').style.display = 'none'
document.getElementById('paramSpace').style.display = 'none'
document.getElementById('param').style.display = 'none'
}
function showOrHideParam(i) {
if (currentParam == i) {
hideParam()
} else {
showParam(i)
}
}
function showFile(id) {
eval('display = document.getElementById("file' + id + '").style.display')
eval('if (display == "none") { document.getElementById("file' + id + '").style.display = "" } else { document.getElementById("file' + id + '").style.display = "none" } ');
}
function showDetails(cnt) {
for (i = 0; i < cnt; ++i) {
eval('document.getElementById("file' + i + '").style.display = ""')
}
}
function hideDetails(cnt) {
for (i = 0; i < cnt; ++i) {
eval('document.getElementById("file' + i + '").style.display = "none"')
}
}
var currentParam = -1;
</script>
{/literal}
{/if}
<title>Page Title</title>
</head>
<body>
{$error_description}<br /><hr>
{$msg}<br /><br />
<i>
Stack Trace:<br />
{$trace}
</body>
</html>
<?php
try {
throw new exception ( 'Throwing an error' );
} catch( Exception $e ) {
try {
throw new SvEx ( $e -> getMessage (), $e -> getFile (), $e -> getLine () );
} catch( SvEx $svEx ) {
$svEx -> Display ( $svEx -> getTrace ());
exit();
}
}
?>
EAvalidator - This class can be used to validate an e-mail address by checking its domain. Categories : PHP , PHP Classes , Email , Regexps Three Cool Classes and One Trick Categories : PHP , PHP Classes , Graphics , Email The Ajax Tree view class fetches data from a db for the requested parent category id. The data is then stored in an array and converted into JSON (Javascript Object Notation) format. This format is then used by JavaScript for populating tree view. Categories : PHP , PHP Classes , Java Script , AJAX , Databases XTemplate, a template class for PHP Categories : PHP Classes , HTML and PHP , PHP A damaged image generator (class) for validating text.
CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart Categories : PHP , PHP Classes , Security , GD image library , Security A Timing Class Categories : PHP , PHP Classes , Date Time [PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more. Categories : PHP , PHP Classes , Databases , MS SQL Server , MySQL A class to put get and post variables in hidden form
elements. Works on scalars, normal arrays, associative
arrays. Categories : Algorithms , Variables , Arrays , PHP , PHP Classes Mssql database Manager Categories : PHP , Databases , MS SQL Server , Classes and Objects , PHP Classes A beginner's session handling class Categories : PHP , PHP Classes , Sessions , Beginner Guides Cool guestbook Categories : PHP , Complete Programs , PHP Classes Recordset Class like ADO Recordset (plus DataBase Splitting feature) using ODBC functions Categories : PHP Classes , ODBC , Databases , PHP The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP , PHP Classes , Debugging Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes , Databases , PHP , MySQL , InnoDB A class to draw real 3D graphics with surface area Categories : Graphics , PHP , PHP Classes