Page 1 of 1

Need help with my error class

Posted: Sun May 29, 2005 12:57 pm
by fastfingertips
I've designed am error class that fit my needs for the moment

Code: Select all

class Error {
	// Class properties
	var $strReturnPage;
	
	// Class: Error
	// Method Desscription: class constructor
	function Error() {		
		// set the handler		
		set_error_handler(array($this, 'CustomHandler'));
	}	
	
	// Class: Error
	// Method Desscription: custom error handler overwrite the default php error handler
	function CustomHandler($errno, $errmsg, $filename, $linenum, $vars) {
		//time stamp for the error
		$dteToday = date("Y-m-d H:i:s (T)");
		
		echo $this->strReturnPage;
		
		// define an assoc array of error string
		$errortype = array (
                E_ERROR           => "Error",
                E_WARNING         => "Warning",
                E_PARSE           => "Parsing Error",
                E_NOTICE          => "Notice",
                E_CORE_ERROR      => "Core Error",
                E_CORE_WARNING    => "Core Warning",
                E_COMPILE_ERROR   => "Compile Error",
                E_COMPILE_WARNING => "Compile Warning",
                E_USER_ERROR      => "User Error",
                E_USER_WARNING    => "User Warning",
                E_USER_NOTICE     => "User Notice"
                );
		
		// set of errors for which a var trace will be saved
	    $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
		
		//depending one the errno
		if ($errno == E_USER_ERROR) {
	    	//this is a critical user error, so i will email it;
	    	
	    	//create the error string
		    $strError = "Date: ".$dteToday."\r\n";
		    $strError .= "Error num: ".$errno."\r\n";
		    $strError .= "Error type: ".$errortype[$errno]."\r\n";
		    $strError .= "Error message: ".$errmsg."\r\n";
		    $strError .= "File: ".$filename."\r\n";
		    $strError .= "Line: ".$linenum."\r\n";
		    
		    //email error
		    $_blnEmailSent = @mail(SITE_MAIL_ERROR,'Error Alert',$strError);
		    
		    //create redirect to the default error page
		    $_SESSION[ERROR] = $errmsg;
		    $_strRedirectLink = "Location: ".SITE_PATH."/error.php";	
			$this->strReturnPage = null;	
			header($_strRedirectLink);					
			die();
		}
		else {
			//i have a warning so i will just load the session with the error
	    	$_SESSION[ERROR] = $errmsg;	
	    		    	
			//I have a return page so i must execute the redirect
			$_strRedirectLink = "Location: ".SITE_PATH.$this->strReturnPage.".php";	
			$this->strReturnPage = null;	
			header($_strRedirectLink);					
			die();
		}
	}
}
Unfortunatelly there is way to set the return page, especially $this->objError->strReturnPage = "" is not working.

After some research i had to modify my code to look like:

Code: Select all

class Error {
	// Class properties
	var $strReturnPage;
	
	// Class: Error
	// Method Desscription: class constructor
	function Error() {		
		// set the handler		
		
	}

	function SetHandler() {
		set_error_handler(array($this, 'CustomHandler'));
	}
and when i want to trigger an error i'm making:

Code: Select all

$objError->strReturnPage = "page.php";
$objError->SetHandler();
trigger_error("An error has been triggered",E_USER_ERROR);
Unfortunatelly as you may notice this is not the ideal answerm, so do you have any suggestion please?

Posted: Sun May 29, 2005 8:23 pm
by Skara
change this
$this->objError->strReturnPage = ""
to this
$this->strReturnPage = ""
???

Code: Select all

class Error {
    var $strReturnPage;

    function Error($page,$msg,$type) {
        $this->strReturnPage = $page;
        set_error_handler(array($this, 'CustomHandler'));
        trigger_error($msg,$type);
    }
//...
$objError = new Error("page.php","An error has been triggered",E_USER_ERROR);
That should work nicely. *shrugs*

Posted: Mon May 30, 2005 12:25 am
by fastfingertips
That is a final solution, i think is better then to use constructor every time.

Code: Select all

<?php

/********************************************************************************
* Author: Administrator
* Date: Sun May 29 11:13:28 PDT 2005
* Description: class error will manage all errors triggered and it will email some
********************************************************************************/

class Error {
	// Class properties

	// Class: Error
	// Method Desscription: class constructor
	function Error() {		
		// set the handler	
		set_error_handler(array($this, 'CustomHandler'));		
	}
	
	// Class: Error
	// Method Desscription: custom error handler overwrite the default php error handler
	function CustomHandler($errno, $errmsg, $filename, $linenum, $vars) {
		//time stamp for the error
		$dteToday = date("Y-m-d H:i:s (T)");
		
		echo $this->strReturnPage;
		
		// define an assoc array of error string
		$errortype = array (
                E_ERROR           => "Error",
                E_WARNING         => "Warning",
                E_PARSE           => "Parsing Error",
                E_NOTICE          => "Notice",
                E_CORE_ERROR      => "Core Error",
                E_CORE_WARNING    => "Core Warning",
                E_COMPILE_ERROR   => "Compile Error",
                E_COMPILE_WARNING => "Compile Warning",
                E_USER_ERROR      => "User Error",
                E_USER_WARNING    => "User Warning",
                E_USER_NOTICE     => "User Notice"
                );
		
		// set of errors for which a var trace will be saved
	    $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
		
		//depending one the errno
		if ($errno == E_USER_ERROR) {
	    	//this is a critical user error, so i will email it;
	    	
	    	//create the error string
		    $strError = "Date: ".$dteToday."\r\n";
		    $strError .= "Error num: ".$errno."\r\n";
		    $strError .= "Error type: ".$errortype[$errno]."\r\n";
		    $strError .= "Error message: ".$errmsg."\r\n";
		    $strError .= "File: ".$filename."\r\n";
		    $strError .= "Line: ".$linenum."\r\n";
		    
		    //email error
		    $_blnEmailSent = @mail(SITE_MAIL_ERROR,'Error Alert',$strError);
		    
		    //create redirect to the default error page
		    $_SESSION[ERROR] = $errmsg;
		    $_strRedirectLink = "Location: ".SITE_PATH."/error.php";	
			$this->strReturnPage = null;	
			header($_strRedirectLink);					
			die();
		}
		else {
			//i have a warning so i will just load the session with the error
	    	$_SESSION[ERROR] = $errmsg;	  	
			//I have a return page so i must execute the redirect
			$_strRedirectLink = "Location: ".SITE_PATH.'/'.$_SESSION[ERROR_PAGE].".php";
			$_SESSION[ERROR_PAGE] = "";	
			header($_strRedirectLink);					
			die();
		}
	}
}

?>