1

I currently handle errors during AJAX requests in a manner similar to this:

try {
    // code

    if (some_error_condition) {
        throw new \Exception('error');
    }

    // other code

    if (some_other_error_condition) {
        throw new \Exception('other error');
    }

    // more code

    $response = array(
        'success' => TRUE,
        'data'    => 'stuff here'
    );
} catch (Exception $e) {
    $response = array(
        'success' => FALSE,
        'error'   => $e->getMessage()
    );
}

header('Content-Type: application/json');
echo json_encode($response);

My question is: is there a better way to handle multiple possible error conditions than this, while still adhering to DRY principles? I think this method is much cleaner and easier to follow than giant nested if/else messes, but it's a little reminiscent of goto code.

Perhaps an OOP way?

4
  • are you using any library? Most of the frameworks (jQuery, Mootools etc.) will have onSuccess/onFailure methods that you can use without having to reinvent the wheel. Commented Jan 5, 2012 at 21:19
  • @julio: I'm interested in how to handle the errors in PHP, not JavaScript. Commented Jan 5, 2012 at 21:20
  • 1
    look here Commented Jan 5, 2012 at 21:20
  • @devdRew: Thank you, but that is not really relevant to what I'm asking. I'm interested in best practices for how to structure the PHP code to be able to neatly and efficiently handle multiple error conditions. Commented Jan 5, 2012 at 21:24

1 Answer 1

2

it is completely valid solution for me, except you could use different exception classes for your exception and encapsulate actual logic in some object, like

class Handler {
      //this function executes code and throws exception - no error handling logic.
     public static function doSomeCode() { 
         (...)
         return $response;
     }
}

try {
    $response = Handler::doSomeCode();
    renderResponse();
} catch (SomeError $e) {
     $err = 'some error';
     renderError($err);
} catch (Exception $e) {
     header('500 Internal Server Error'); //this is pseudo code!
} 

your exception classes (except generic Exception) could handle rendering errors, Exception class would trigger 500 (it should never happend). This way you separate actual code execution from error handling, and with proper exceptions object model dont repeat yourself with error handling.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting. Would you have a different custom Exception class for each relevant HTTP response code (not authorized, bad request, etc.)? Or just one to handle all codes?
it depends if error handling logic differs - as long as its the same I would use one exception class (i.e. HttpException), when logic differs you can always refactor to separate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.