views:

68

answers:

1

I currently have two class hierarchies: one for handling errors and another for logging them. I am wondering 1) whether it is advantageous to delegate error handling to a class separate from the error handler and 2) whether what I have is a good design.

Here is what I have:

class ErrorHandler {
  public __construct(ErrorLogger $ErrorLogger) {
    ...
    $this->setErrorPageURL('/error.php');
  }

  public function setErrorPageURL($errorPageURL);

  public function handle() {
    ...
    $this->ErrorLogger->log($errorNumber, $errorMessage, $errorFile, $errorLine);
    header("Location: $this->errorPageURL");
  }
}

class ErrorLogger {
  abstract protected function log($errorNumber, $errorMessage, $errorFile, $errorLine);

  // Overridable.
  protected function getExtraLogMessage() {
    return null;
  }
}

class EmailerErrorLogger extends ErrorLogger {
  public function __construct($toEmailAddress, $fromEmailAddress) {
    ...
  }

  protected function log($errorNumber, $errorMessage, $errorFile, $errorLine) {
    // collect error information and various pieces of information
    mail(...);
  }
}

register_shutdown_function(array(new ErrorHandler(new EmailerErrorLogger()), 'handle'));
+1  A: 

I think this structure makes sense, because your error handler has to do certain other jobs than just logging the error.

The loggers themselves could implement the chain of responsibility pattern

Dario