Blog

Error Handling – Update: Exceptions to be used

I want to make more use of error handling rather than just straight die()’s – so I’m working on implementing http://codex.wordpress.org/Function_Reference/WP_Error within 2.0 for different cases. The only thing I’m trying to work out is not the implementation of the error, but the handling of the error on the other side. This may add an additional line or two for each place pods_query is used, as well as the PodsAPI. There are many benefits with being able to see real errors via PHP than just die()’ing but I’m wondering what other developers think of that.

Put your two cents in, I could use it before I get in too deep. Right now, we have little to no error handling, only PodsAPI has an ‘oh_snap’ function, which depending on a class variable it will die or throw an exception.

What do you guys prefer? WP_Error, Exception, or die()? Or maybe something else?

UPDATED @ 12:31pm on March 14th

We’re going with Exceptions, see below for the new global function used.

function pods_error ($error, $obj = null) {
    $display_errors = false;
    if (is_object($obj) && isset($obj->display_errors) && true === $obj->display_errors) {
        $display_errors = true;
    }
    elseif (is_bool($obj) && true === $obj) {
        $display_errors = true;
    }
    if (false !== $display_errors) {
        throw new Exception($error);
        return false;
    }
    die($error);
}