Re: [PHP] Re: poll: howto do informative error handling without the fatalities

[email protected] (Richard Quadling)
Newsgroups php.general
Message-ID <[email protected]>
On 9 February 2010 11:48, Nathan Rixham <[email protected]> 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
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.