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

From: Date: Tue, 09 Feb 2010 12:01:25 +0000
Subject: Re: Re: poll: howto do informative error handling without the fatalities
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-301900@lists.php.net to get a copy of this message
On 9 February 2010 11:48, Nathan Rixham <nrixham@gmail.com> wrote: > 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! > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > I have extended the standard exception class to send me an email whenever an exception occurs. I treat all errors on the live system as exceptions. They shouldn't occur. If they do, I've missed something. But at least I'll get a near instant notification of the issue, along with the stack and all the params involved. Very useful. -- ----- Richard Quadling "Standing on the shoulders of some very clever giants!" EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling

« previous php.general (#301900) next »