PEAR_Error, ErrorStack, Exception, and compatibility
| From: | Justin Patrin | Date: | Tue, 22 Jun 2004 20:47:15 +0000 |
| Subject: | PEAR_Error, ErrorStack, Exception, and compatibility | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-31122@lists.php.net to get a copy of this message | ||
I think this discussion may have been lost in the multitude of messages in other threads, so I'm starting a new thread.
There has been lots of talk about Exceptions in PEAR. Many people are saying that PHP5 PEAR classes should not use PEAR_Error at all and only throw exceptions. This can cause many problems:
1) The code is not compatible with PHP4
2) Some people believe that Exceptions are supposed to be for Exceptional circumstances, not general error handling
3) As per 2), it forces the user to handle errors in a certain way.
For this reason, I propose the following:
Backwards compatibility would be very simple by simply allowing a mechanism (through PEAR_Error/Stack) to throw the error as an exception or return a PEAR error. Here's what would happen.
Package1 likes PEAR_Errors. It sets (pushes) the error mode to PEAR_ERROR_RETURN. It uses an instance of Package3 which then has an error. Package3 makes a call as such:
return $this->errorStack->push(...);
ErrorStack smartly creates a PEAR_Error and returns it. Package3 then returns the PEAR_Error and Package1 is happy.
Package2 likes Exceptions. It sets (pushes) the error mode to PEAR_ERROR_EXCEPTION. It uses an instance of Package3 which then has an error. Package3 makes the same call as above. Instead od returning a PEAR_ERROR, ErrorStack throws a PEAR_Exception (with the same data). THe exception gets popped up to Package3, and then pops back up to Package2, which is then happy. There is one more level in the backtrace of the Exception, but this seems a very small price to pay for such compatibility. In addition, the PEAR_Exception could have special funcitonality to remove / not show the errorStack call if this is really important to people.
Questions / Comments?
--
paperCrane <Justin Patrin>