how to suppress exception
Posted: Wed Aug 08, 2007 9:23 am
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
PHPUnit function
Utility class function
When executing phpUnit script, it displays a large exception object with the following message.
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.
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();
}
}
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