Re: poll: howto do informative error handling without the fatalities

[email protected] (Nathan Rixham)
Newsgroups php.general
Message-ID <[email protected]>
Rene Veerman wrote:
> Hi,
> 
> I'm looking for a strategy to do informative error handling at all
> levels of my code, yet keep these errors non-fatal as often as
> possible.

error_log - for logging errors
throw Exception - for show stoppers
try/catch - for when you can handle a potential show stopper

custom error logging / messaging can easily be achieved with something
like this:

<?php

class Messenger
{

  private static $_messages = array();

  public static function addMessage( $string )
  {
    self::$_messages[] = $string;
    if( $string instanceof Exception ) {
      echo self::report();
    }
  }

  public static function report()
  {
    return implode( PHP_EOL , self::$_messages );
  }

}

set_exception_handler( 'Messenger::addMessage' );

Messenger::addMessage( 'little error report 1' );
Messenger::addMessage( 'little error report 2' );
Messenger::addMessage( 'little error report 3' );
throw new Exception( 'this will stop the script' );
// exception will kill the script; if you comment it out
// or wrap it in a try catch then you can keep going..
Messenger::addMessage( 'little error report 4' );
// try catch exceptions and report them like this..
try {
  throw new Exception( 'we could catch this' );
} catch ( Exception $e ) {
  Messenger::addMessage( $e );
}
Messenger::addMessage( 'little error report 5' );
// and when your done just echo it out or save it or..
echo Messenger::report();


Regards!
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.