Home » PEAR » PEAR » Bug #5331
PEAR_Exception should take any Exception as cause
Details
| Submitted | 2005-09-08 09:32 UTC |
|---|---|
| From | info at adaniels dot nl |
| Assigned | cellog |
| Status | Closed |
| Package | PEAR |
| PHP Version | 5.0.5 |
| OS | Linux |
| Roadmaps | (Not assigned) |
Comments
[2005-09-08 09:32 UTC] info at adaniels dot nl
Description:
------------
Related to bug #5330
PEAR_Exception should take any Exception as cause, not only PEAR_Exceptions.
Test script:
---------------
<?php
set_include_path(get_include_path() . ":lib:lib/class:lib/Pear:cfg");
require_once('PEAR/Exception/ext.php');
class myException extends PEAR_Exception_ext {} #PEAR_Exception_ext is PEAR_Exception with fix for getCauseMessage
throw new myException("I would like to see this message", array(new Exception('A sub exception'), new PEAR_Exception_ext('A sub PEAR exception')));
?>
Expected result:
----------------
<b>Fatal error</b>: Uncaught <table border="1" cellspacing="0">
<tr><td colspan="3" bgcolor="#ff9999"> <b>myException</b>: I would like to see this message in <b>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php</b> on line <b>7</b></td></tr>
<tr><td colspan="3" bgcolor="#ff9999">- <b>Exception</b>: A sub exception in <b>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php</b> on line <b>7</b></td></tr>
<tr><td colspan="3" bgcolor="#ff9999">-- <b>PEAR_Exception_ext</b>: A sub PEAR exception in <b>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php</b> on line <b>7</b></td></tr>
<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>
<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td><td align="center" bgcolor="#cccccc"><b>Function</b></td><td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>
<tr><td align="center">0</td><td>unknown()</td><td>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php:7</td></tr>
<tr><td align="center">1</td><td>{main}</td><td> </td>< in <b>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php</b> on line <b>7</b><br />
Actual result:
--------------
<b>Fatal error</b>: Uncaught <table border="1" cellspacing="0">
<tr><td colspan="3" bgcolor="#ff9999"> <b>myException</b>: I would like to see this message in <b>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php</b> on line <b>7</b></td></tr>
<tr><td colspan="3" bgcolor="#ff9999">- <b>PEAR_Exception_ext</b>: A sub PEAR exception in <b>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php</b> on line <b>7</b></td></tr>
<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>
<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td><td align="center" bgcolor="#cccccc"><b>Function</b></td><td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>
<tr><td align="center">0</td><td>unknown()</td><td>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php:7</td></tr>
<tr><td align="center">1</td><td>{main}</td><td> </td></tr>
</table>
thrown in <b>/var/www/seabert/webroot/ontwikkeling/djaboo2/test.php</b> on line <b>7</b><br />
[2005-09-08 09:36 UTC] info at adaniels dot nl
Solution
--------------
/**
* Function must be public to call on caused exceptions
* @param array
*/
public function getCauseMessage(&$causes)
{
$trace = $this->getTraceSafe();
$cause = array('class' => get_class($this),
'message' => $this->message,
'file' => 'unknown',
'line' => 'unknown');
if (isset($trace[0])) {
if (isset($trace[0]['file'])) {
$cause['file'] = $trace[0]['file'];
$cause['line'] = $trace[0]['line'];
}
}
$causes[] = $cause;
if ($this->cause instanceof PEAR_Exception) {
$this->cause->getCauseMessage($causes);
}
if (is_array($this->cause)) {
foreach ($this->cause as $cause) {
if ($cause instanceof PEAR_Exception) {
$cause->getCauseMessage($causes);
} elseif ($cause instanceof Exception && false) {
$causes[] = array('class' => get_class($cause),
'message' => $cause->getMessage(),
'file' => $cause->getFile(),
'line' => $cause->getLine());
} elseif (is_array($cause) && isset($cause['message'])) {
// PEAR_ErrorStack warning
$causes[] = array(
'class' => $cause['package'],
'message' => $cause['message'],
'file' => isset($cause['context']['file']) ?
$cause['context']['file'] :
'unknown',
'line' => isset($cause['context']['line']) ?
$cause['context']['line'] :
'unknown',
);
}
}
}
}