Error Handling

I’ve developed and become comfortable with a OO PHP system I’ve developed. I’ve set it up so that I can reuse it on projects. The one thing I feel it is lacking is good error handling. My question is, how do you handle errors in a OO PHP application? How do you pass errors from the business layer to the presentation layer? Specific code examples would be great.

Below is a very rough example of how I handle errors
SomeClass.php



class SomeClass {
   public function someFunc(){
      $errors = Array();
      //do some processing

      if(somethingFails){
         $errors[] = "Something Failed";
      }
      return $errors;
   }

}


Index.php


require_once("SomeClass.php");
$class = new SomeClass();
$errors = $class->someFunc();

if(count($errors>0)){
    $tmpl = new Template(); //templating class
    $tmpl->errors = $errors;
}else{
    //Process application
}

Template.php


$errors = $tmpl->errors;

if(count($errors) > 0){
   foreach($errors as $err){
      //Print Error
   }
}


I think a nice approach (if maybe overcomplicated) would be to have a class (static or injected, depending on your application design) which is purely for logging errors. When you’re running stuff which expects errors to possibly occur, apply a listener on that error class.

Using a static class for ease of example:

class ErrorLogger{
    public static $Errors = array();
    public static $Listeners = array();
    public static function attachListener(iErrorListener $Listener){
        $this->Listeners[] = $Listener;
    }
    public static function reportError($Error){
        $Errors[] = $Error;
        foreach($Listeners as $Listener){
            $Listener->report($Error);
        }
    }
}

Now, whenever an error is reported, the listener class (which can be the class calling the erroring function in the first place) will be alerted whenever an error is caused. It could choose to terminate the erroring function or do something else before continuing.

Depends on the type of error really. If we’re talking low level stuff then I’ll just throw an exception. On the top level there is a handler that catches exception, logs them and gives the user a something-went-wrong page. If the error is something that is part of the application state - eg. a failed validation - I would have the particular model component log the error and have the presentation layer display it. Or in some cases generate and display the error in the presentation layer.

Sorry for being vague, but it’s a wide question.

That’s pretty much the way I handle things too. When I’m throwing an exception, I have an extended Exception class that I can attach observers to. So in a live site I have a logger observer, error page presenter observer, and for some things even an ‘adminAlerter’ observer that sends me an email if some really bad stuff is going on. In a development environment I only use a debugger observer that displays as much information about the exception as it can.

Depends on the type of error really. If we’re talking low level stuff then I’ll just throw an exception

The system needs to be flexible. But for the sake of narrowing the scope of the question.

Lets concentrate specifically on data validation.

Assume I have a Data Validation class which I use to determine if the user input is valid. This is how I would handle it.


class someClass{
public function validateInput(){
$errors = Array();
$dataValidator = new DataValidator();
      if(!$dataValidator->validPhone($this->data['phone']) && ($this->data['phone'] != "")){
            $errors['ssn'] = " Invalid Phone Format";
      }

      if(!$dataValidator->validateUSAZip($this->data['addressZip']) && ($this->data['addressZip'] != "")){
            $errors['zip'] = "Invalid Zipcode Format";
      }
   return $errors;
}
}

Then using the same index file and template file as I originally posted.

I have an extended Exception class that I can attach observers to. So in a live site I have a logger observer, error page presenter observer, and for some things even an ‘adminAlerter’ observer that sends me an email if some really bad stuff is going on. In a development environment I only use a debugger observer that displays as much information about the exception as it can.

This sounds really interesting but I’m having trouble fully grasping the idea. Are you able to post some code examples? That would be really helpful.

Thanks.