Lack of Exceptions in PHP Libraries

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.

This entry was posted in Amazon Web Services, Cloud, PHP, Zend Framework and tagged , , , . Bookmark the permalink.

2 Responses to Lack of Exceptions in PHP Libraries

  1. Pingback: Simple Cloud – Error Handling Not Portable | Rob Olmos

  2. Motin says:

    Can not agree more. I guess you too ended up with something similar to this:

    <?php

    /**
    * Wrapper around AmazonSDB that will properly throw exceptions on non-"isOK" requests
    *
    * @author motin
    * @license MIT
    */
    class AmazonSDBWrapper extends AmazonSDB
    {

    public function authenticate($action, $opt = null, $domain = null, $signature_version = 2, $redirects = 0)
    {
    $response = parent::authenticate($action, $opt, $domain, $signature_version, $redirects);
    if (!$response->isOK())
    {
    throw new AWSException($response);
    }
    }

    }

    /**
    * Simple formatting of AWS responses with Error data
    *
    * @author motin
    * @license MIT
    */
    class AWSException extends Exception
    {

    public $response;

    public function __construct(&$response, $message = “”, $code = null, $previous = null)
    {
    $this->response = $response;
    //var_dump($response);
    $message = (string) $response->body->Errors->Error->Code;
    $message .= “: ” . (string) $response->body->Errors->Error->Message;
    $code = 0;
    parent::__construct($message, $code, $previous);
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>