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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 9 February 2010 11:48, Nathan Rixham <nrixham@xxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux