Why are new PHP libraries still failing to fully utilize exceptions? Is it PHP4 compatibility? Speed? Maintain the API across exception-less languages such as C? Simply because most developers still don’t effectively use exceptions?
It’s a shame that PHP exceptions are still not being employed in recent libraries despite exceptions were initially announced over 5 years ago. A recent and frustrating experience was with researching and working with PHP libraries for Amazon S3.
We could go into the semantics of “exception” & “error” and when they should be thrown but let’s skip that and sum it simply as this: If the library is not able to fully complete the requested action then it needs to throw an exception detailing why.
In other words, if the library cannot save my image to Amazon S3 then it needs to throw an exception with Amazon’s error message. If it cannot delete my image it needs to throw an exception. If it cannot update the meta-data of my image it needs to throw an exception.
I researched three libraries:
None of them throw an exception on error with Amazon S3. The only exceptions thrown are for validation like the bucket name or maybe some lower error in the Zend HTTP request class.
For each of these I researched into the “put/create object” method:
Zend Framework’s Amazon S3: Only returns true or false. I was forgiving though because that piece of the library was probably designed earlier in the framework and so maintained API. I also was having trouble actually getting the an error message or the entire response object as bug reported http://framework.zend.com/issues/browse/ZF-9436
Simple Cloud: Uses the Amazon S3 library of Zend Framework and simply returns the return value of it ie. true or false. (This is actually a problem I found with Simple Cloud’s portability with other adaptors)
AWS SDK for PHP: Returns a CFResponse object that requires a isOK() method check.
I do not want to test if the library call was successful or not in some C-like true/false or some PHP4-style PEAR::isError() manner. I do not want to test for “false” and then have to ask the library for the error. I want to consolidate my error handling; use exceptions in your libraries please.
I understand it’s unfair to group all libraries into this article when this article is only about AWS S3 libraries, even though there are other recent libraries that still don’t use exceptions, but this is mainly to promote awareness.