SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Error Handling

  1. #1
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Code PHP:
     
    class SomeClass {
       public function someFunc(){
          $errors = Array();
          //do some processing
     
          if(somethingFails){
             $errors[] = "Something Failed";
          }
          return $errors;
       }
     
    }
    Index.php
    Code 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
    Code PHP:
    $errors = $tmpl->errors;
     
    if(count($errors) > 0){
       foreach($errors as $err){
          //Print Error
       }
    }
    Follow Me On Twitter: BryceRay

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    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.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    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.
    aaron-fisher.com - PHP articles and more

  5. #5
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP Code:
    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.
    Follow Me On Twitter: BryceRay


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •