Page 1 of 1

how to suppress exception

Posted: Wed Aug 08, 2007 9:23 am
by raghavan20
i have an utility class which has a public static method, 'updateRejectedStatusInALAN'. This method is responsible for marking an 'isRejected' field 1 in some table. There is no problem with functionality as it works properly. The method returns TRUE if it successfully marks that field as 1 or it throws an exception of type 'ExceptionHandler' which is-a Exception class. The ExceptionHandler class is a child class of Exception and it holds a var, fullDetails which stores exception details along with backtrace information.

I am now writing an unit test to make sure that an exception is raised if not able to mark 1 for 'isRejected' field for a given row. When I execute the PHPUnit code, it sends/prints out a large debug_back_trace information which I am not printing at all from any of the scripts involved.

ExceptionHandler class

Code: Select all

<?php

class ExceptionHandler extends Exception
{
	
	private $date;
	private $operation;
	private $errorCode;
	private $exceptionSummary;
	private $fullDetails;
	
	/**
	 * 
	 * 
	 *
	 * @param unknown_type $message
	 * @param unknown_type $operation
	 * @param unknown_type $code:	
	 * available codes: 
	 * 	FORMAT: MainErrorCode_ErrorGroup
	 * 	Error Groups:
	 * 	_BLE - Business Logic Error
	 * 	
	 * 	Main Errors:
	 * 	OI - Order Invalid
	 * 
	 * 	Ex: Code = OI_BLE
	 * 
	 * */
	public function __construct( $message, $operation, $errorCode = FALSE ) {
	
	 	// make sure everything is assigned properly
	 	parent::__construct($message);
	 	$this->date = date( "Y-m-d H:i:s" );
	 	$this->operation = $operation;
	 	$this->errorCode = $errorCode;
	 	
		$this->exceptionSummary = "
		
			<br />
			<div class = 'plainTable' style = 'padding:20px;'>
				<div>
					<b>Error occurred during the Process / Operation: '{$this->getOperation()}</b>'
				</div> 		
				Error Details:<br /><br />			
					'<b>{$this->getMessage()}</b>'<br /><br /> 
					in '<b>{$this->getFile()}</b>'<br />
				 	at line, '<b>{$this->getLine()}</b>' <br />
					on '<b>{$this->getDate()}</b>'. 
			</div>
		
		
		";
		
		
		$this->fullDetails = $this->exceptionSummary.
		"<br /><br /><br />
		<div class = 'plainTable' style = 'padding:20px;'>
			<div>Debug Trace:</div><pre>"
			.var_export( debug_backtrace(), TRUE )
		."</div>";
		
	 	
	 	
	 
	}
	
	// custom string representation of object
	public function __toString() {
		 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
	}
	
	public function getDate (){
		return $this->date;
	}
	public function getOperation (){
		return $this->operation;
	}
	public function getExceptionSummary(){
		return $this->exceptionSummary;
	}
	public function setExceptionSummary( $input ){
		$this->exceptionSummary = $input;
	}
	public function getErrorCode(){
		return $this->errorCode;
	}

	public function reportErrorByMail( $fullDetails = TRUE ){
		
		##echo "reached first line of reportErrorByMail() !!!";
		//compose message
		$message = $this->composeMessage( $fullDetails );
		
		
		
	}
		
	
	private function composeMessage( $fullDetails ){
		
		if ( $fullDetails === TRUE )	$message = $this->fullDetails;
		else $message = $this->exceptionSummary;
		
		$message = "
			<html>	
				<head>	
					<style type = 'text/css'>
						.plainTable{
							background-color:white; border:1px solid maroon; border-width:4px 1px 4px 1px; font-size:1.0em;
						}
						.plainTable td, th{
							padding:2px 7px 2px 7px; vertical-align:top;
						}
						.plainTable td{
							color:black; padding:2px 7px 2px 7px;
						}
						.plainTable th{
							padding: 7px;
						}					
					</style>
				</head>
				<body>
					$message
				</body>
			</html>
		
		";
		return $message;		
		
	}


}


?>

PHPUnit function

Code: Select all

class ALANHelperTest extends PHPUnit_Framework_TestCase{
                 public function testUpdateRejectedStatusInALAN2(){
		
		$isExceptionRaised = FALSE;
		try{
			$isUpdated = ALANHelper::updateRejectedStatusInALAN( '058-8521844-6077102', '1853025771' );
		}catch( ExceptionHandler $e ){
			##echo 'exception handled called'; exit;
			$isExceptionRaised = TRUE;
		}
		if( $isExceptionRaised === FALSE ) $this->fail();
		
		
	}

}
Utility class function

Code: Select all

	
public static function updateRejectedStatusInALAN( $channelOrderId, $productIndentifier, $isMedia = FALSE ){
		
		$dbManager = new DBManager();
		$conn = $dbManager->getALANConnection( TRUE );
		$processResult = FALSE;
		
		$query = "
		
			UPDATE
				`oms`.`cdfOrders`
			SET
				`isRejected` = 1
			WHERE
				`OrderID` = '$channelOrderId'
			AND
				`ISBN` = '$productIndentifier'
				
		";
		
		$conn->query ( $query );
		$error = $conn->error;
		$affectedRows = $conn->affected_rows;
		
		if ( $affectedRows != 1  ){
			throw new ExceptionHandler(
				"The updated rows is not equal to one. The number of rows updated is $affectedRows.",
				"Process of updating isRejected field in ALAN's cdfOrders while cancelling the order
				with id, $channelOrderId with the product id, $productIndentifier.<br>
				The error occured is, $error.<br> The query is: $query"
			);
		}else{
			$processResult = TRUE;
		}
		
		##if ( get_class ( $conn )  ==  "mysqli" ) $conn->close();
		return $processResult;
		
	}

When executing phpUnit script, it displays a large exception object with the following message.

Code: Select all

[root@mail helpersTest]# php ALANHelperTest.php > log.1
PHP Fatal error:  Nesting level too deep - recursive dependency? in /var/www/html/CDF/backend/classes/core/ExceptionHandler_CL.php on line 60
I do not really want to see this ExceptionHandler object printed out and I cannot understand the error it prints. I would appreciate if somebody can explain me what this is all about.

Posted: Wed Aug 08, 2007 1:20 pm
by feyd
The error would suggest you have an infinite or very deep recursion. Nothing in the code pops out as a likely source of it however.

Posted: Wed Aug 08, 2007 2:43 pm
by ev0l
What is on line 60 in /var/www/html/CDF/backend/classes/core/ExceptionHandler_CL.php ?

If it this line?

Code: Select all

var_export( debug_backtrace(), TRUE )
If so var_export does not handle circular references. You should use var_dump instead.

Posted: Thu Aug 09, 2007 3:47 am
by raghavan20
ev0l wrote:What is on line 60 in /var/www/html/CDF/backend/classes/core/ExceptionHandler_CL.php ?

If it this line?

Code: Select all

var_export( debug_backtrace(), TRUE )
If so var_export does not handle circular references. You should use var_dump instead.

hi ev0l, i may agree that var_export does not handle circular references. i am using var_export because I want to get the information of debug_backtrace and add it to the exception detail string and send as email to admin. but i cannot do this with var_dump as it would just print out on the screen but var_export will help capture the information and would store in a variable or in memory. thanks for your help.

EDIT: also would like to let you know that the ExceptionHandler class works as it is. This only does not work with pHPUnit.

Posted: Thu Aug 09, 2007 12:54 pm
by ev0l
raghavan20 wrote:[
...
i am using var_export because I want to get the information of debug_backtrace and add it to the exception detail string and send as email to admin. but i cannot do this with var_dump as it would just print out on the screen but var_export will help capture the information and would store in a variable or in memory. thanks for your help.

Use output buffering like soo ....

Code: Select all

ob_start();
var_dump($whatever);
$var_dumpOutput = ob_get_contents();
ob_end_clean();