Error handling

From Achievo/ATK Wiki

Jump to: navigation, search

ATK Howto: Error handling

Complexity: easy
Author: Mark Wittens

List of other Howto's

Preface

If an error occurs in ATK it can be sent by email to you or anyone who might be interested so the error can be fixed as fast as possible. It is also possible to log the error in Zend Platform and you could even do something entirely different if you like. How to enable an existing error handler and how to create your own custom handler will be explained in this how-to.

Enable error handling

The usual way to tell ATK to mail the error would be to set the config value $config_mailreport with a valid email address.

  $config_mailreport = "someone@somewhere.com";

If you would like to have multiple error handlers or you want to handle it differently you can use the following config value:

  $config_error_handlers = array('mail'=>array('mailto'=>'someone@somewhere.com'));

This has the same result as the original code, the array with the 'mailto' key will be passed to the error handler but we will get to this later. It already gets a bit more interesting when we add another error handler:

  $config_error_handlers = array('ZendPlatform', 'mail'=>array('mailto'=>'someone@somewhere.com'));

From now on, not only will the error be sent by mail but it also gets logged in Zend Platform! (if Zend Platform is available offcourse)

Create a new error handler

Suppose you would like to do something different in case of an error, you might want to send it to all of your friends by e-card or do something usefull with it. To do so you can create your own error handler.

First, create a file named class.atk<your error handler name here>errorhandler.inc in atk/errors/. In this file you'll need to create a new class named atk<your error handler name here>ErrorHandler and derive it from atkErrorHandlerBase. It's easier to just look at the following example:

  class atk<your error handler name here>ErrorHandler extends atkErrorHandlerBase
  {   
    public function handle($errorMessage, $debugMessage)
    {
      // Do something interesting with the error here
    }
  }

If you add your handler's name to the config the handle() function of your class will be called in case of an error:

  $config_error_handlers = array('<your error handler name here>', 'ZendPlatform', 'mail'=>array('mailto'=>'someone@somewhere.com'));

If you want to pass config values to the error handler this can be done by adding those as an array value to the key you just added:

  $config_error_handlers = array('<your error handler name here>' => array('value1'=>1, 'value2'=>2) , 'ZendPlatform', 'mail'=>array('mailto'=>'someone@somewhere.com'));

The values will be accessible from the error handler through the $this->params member variable. For example:

  class atk<your error handler name here>ErrorHandler extends atkErrorHandlerBase
  {   
    public function handle($errorMessage, $debugMessage)
    {
      echo $this->params['value1'];
    }
  }
Personal tools
Navigation