Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On 9 February 2010 22:25, wrote: > On Tue, 9 Feb 2010 16:09:05 +, rquadl...@googlemail.com (Richard > Quadling) wrote: > >>On 9 February 2010 14:20, Ashley Sheridan wrote: >>> >>> On Tue, 2010-02-09 at 09:19 -0500, Robert Cummings wrote: >>> >>> Richard wrote: >>> > Hi, >>> > >>> >> I have extended the standard exception class to send me an email >>> >> whenever an exception occurs. >>> > >>> > I did that once. Once being the operative word... :-) Ended up with >>> > tens of thousands of emails one morning. At first I thought... "Wow, >>> > maybe my popularity has grown somewhat". But it hadn't. >>> >>> I have something similar... a cron job that checks the error_log file >>> every 10 minutes and sends me the contents if any exist. I also set a >>> special header so I can be sure it's not spam and appropriately route it >>> into my mail folder maze, Much less spammy :) >>> >>> Cheers, >>> Rob. >>> -- >>> http://www.interjinn.com >>> Application and Templating Framework for PHP >>> >>> >>> Real developers don't have errors in their code; they're undocumented >>> features ;) >>> >>> Thanks, >>> Ash >>> http://www.ashleysheridan.co.uk >>> >>> >> >>So, no documentation AND bugs!!! Gee. I really wouldn't want to rely >>on that code base! > > So you don't use (or work with) any Microsoft product? > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > The closest I get is MSSQL which has BOL (Books OnLine). Its enough. Occasionally MS Excel VBA which is fairly well documented and always accessible via a COM interface (so self documenting more or less). But point taken. And only 12 MS critical updates today! Woo Hoo! -- - 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
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On Wed, Feb 10, 2010 at 3:04 AM, Paul M Foster wrote: > $d = funcD($q); > // tests if necessary > $c = funcC($p); > // tests if necessary > $b = funcB($c); > // tests if necessary > $r = funcA($b, $d); > You're right. I knew when i was posting my last reply yesterday that i had made things probably too complicated. But after sleeping and a look at real code which is to work with my improved-err-handling, i've come up with a new construct, that i think will fit the bill for now. I'm going to use the "result meta-array" instead of "plain result values", because that - completely equalizes the syntax for detecting bad results, and prevents doing a lookup with a function to a value that is also the "bad-result" return value for the lookup function. - is much easier to read, than checking what the "bad-result" value is for a lookup function. - allows me to redirect the error info from a called function inside the calling function - allows infinite expansion of (meta-)communication between calling and called functions. So while i'm dumping the funcA (defaults($onError_pValue), $p) crap, i'm retaining most of the other ideas in my previous post to this thread. the calling function does this, inside 3x foreach: // if not done yet, resolve the fields for this table=>hit that require looking-up if (!$sqls[$tableName]['hasPreInserts']) { foreach ($tableCmd['preInsertFields'] as $fieldName=>$fieldLookupCmd) { $r = maintenance__lookupPreInserts ($wm, $fieldLookupCmd); if (!good($r)) { $tableCmd['invalidPreInserts'] = true; } else { $sqls[$tableName]['preInserts'][$fieldName] = result($r); } } } if (!array_key_exists('invalidPreInserts', $tableCmd)) { // add the fields for this table for this hit: . and the helper functions: function maintenance__lookupPreInserts (&$wm, $fieldLookupCmd) { startErrorContext(); // to catch php E_NOTICE, E_WARNING, etc $flcp = explode ('::', $fieldLookupCmd); if (count($flcp)!=3) { return badResult(E_USER_ERROR, array( 'msg' => 'Need 3 counts of \'::\'', 'vars' => array (0=>array('$fieldLookupCmd', $fieldLookupCmd)) )); } $section = $flcp[0]; $searchInSection = array(); $criteria = explode (',,', $flcp[1]); foreach ($criteria as $idx1 => $criterium) { $cs = explode('===',$flcp[1]); if (count($cs)!=2) return badResult (E_USER_ERROR, array( 'msg' => 'Any criterium (after first "::", between ",,") needs to be like this:'."\n". 'fieldName===searchValue', 'vars' => array (0=>array('$fieldLookupCmd', $fieldLookupCmd)) )); $searchInSection = array_merge ($searchInSection, array( $cs[0] => $cs[1] )); } $pathInSection = explode(',,', $flcp[2]); foreach ($wm[$section] as $idx => $fields) { $gotIt = true; foreach ($searchInSection as $fn => $fv) { if ($fields[$fn]!=$fv) { $gotIt = false; break; } } if ($gotIt) { return chase ($wm[$section], $pathInSection); } } } function chase ($arr, $indexes) { startErrorContext(); $r = $arr; foreach ($indexes as $idx) { if (is_array($r)) { $r = $r[$idx]; } else { return badResult (E_USER_ERROR, array( 'msg' => 'Could not walk the full tree', 'vars' => array( 0=>array('$arr', $arr), 1=>array('$indexes', $indexes) ) )); } } return goodResult($r); } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On Tue, Feb 09, 2010 at 11:38:42PM +0100, Rene Veerman wrote: > Well, i've thought of a few ways to allow localized overriding of > values returned by functions in case of an error. > > I have yet to figure out what the exact advantages are of this > code-standard, but the overhead seems acceptable and i recon this, > when done, will beat the "trigger_error()-try-catch" paradigm. > I'd like to hear your comments. > > // imo unsafe & prone to fatalities: > $r = funcA ( funcB ( funcC ($p) ), funcD ($q) ); I wasn't going to comment, but seriously, Rene, I would not make it a habit of calling functions within the parameters of other functions. Among other things, it obviously creates a whole host of the problems you're talking about, and it makes your code hard to debug and read. The only way I'd normally do something like this is if I were using native PHP functions and the possible failure of the functions wouldn't matter. Rather, I'd do something like this: $d = funcD($q); // tests if necessary $c = funcC($p); // tests if necessary $b = funcB($c); // tests if necessary $r = funcA($b, $d); Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
Well, i've thought of a few ways to allow localized overriding of values returned by functions in case of an error. I have yet to figure out what the exact advantages are of this code-standard, but the overhead seems acceptable and i recon this, when done, will beat the "trigger_error()-try-catch" paradigm. I'd like to hear your comments. // imo unsafe & prone to fatalities: $r = funcA ( funcB ( funcC ($p) ), funcD ($q) ); // "safe"; make all notices, warnings and errors from any level // in the call-chain fully visible, identifiable, and "survivable/fudgeable" // by code at (all) higher levels in the call-chain. // [disclaimer] the code below here doesn't pass 3rd-level (funcC()) // (meta-)results back to the first level (funcA()) yet, but this code // is a start towards that; $r = funcA (defaults(array(0=>'[funcB failed]',1=>'funcD failed'))), funcB (defaults($onError_cCallvalue), funcC (defaults($onError_pValue), $p) ), funcD (defaults($onError_qValue), $q) ); function funcA ($defaults, $returnedByFuncB, $returnedByFuncD) { $returnedByFuncB = workWith ($defaults, $returnedByFuncB, 0); $returnedByFuncD = workWith ($defaults, $returnedByFuncD, 1); //all parameters (with idx>0) are meta-arrays containing the "parameter-value" and all meta-info. // workWith() always returns the actual parameter-value in $p['workValue']; //if any parameter $p (with idx>0) is not a meta-array, // workWith($d, $r, $n) turns it into a meta array( // 'workValue' => $p // ); //if a param is a meta-array $p, and it contains an ['errors'] subarray, // workWith() sets by-reference $p['workValue'] to one of these (order of trying): // $defaults['onError'][$n] (localized override) // $p['onErrorDefault'], (functions own default value for when an error occurs) //first call to workWith(,,0) will call beginErrorContext() //beginErrorContext() will increase a global idx $callID (int), // used to list all notices, warnings and errors, and their details in // the global variable $callsErrors[$callID][$errorIdx++] = array( // 'errorMsg' => 'for coders' // (optional:)'userErrorMsg' => 'for laymen' // 'backtrace' => array() // ) // the never-fatal error handler specified with set_error_handler() will // call getDebugInfo() and append full error info into // $callsErrors[$callID][$errorIdx++] //forwardDefaults() clones all meta-details for // re-used parameters into a new $defaults array to be used // by funcE(). //the 2nd parameter for forwardDefaults lists which parameter // passed deeper refers to which parameter received by this here function. //the 3rd parameter for forwardDefaults is used to specify new defaults. $returnedByFuncF = funcF('blablasetting'); $x = funcE ( forwardDefaults($defaults, array(0=>0,2=>1), array(1 => 'defaultblabla')), $returnedByFuncB, $returnedByFuncF, $returnedByFuncD ); return goodResult($x); } function funcE ($defaults, $returnedByFuncB, $returnedByFuncF, $returnedByFuncD) { $returnedByFuncB = workWith ($defaults, $returnedByFuncB, 0); $returnedByFuncF = workWith ($defaults, $returnedByFuncF, 2); $returnedByFuncD = workWith ($defaults, $returnedByFuncD, 2); // do a call that might raise E_WARNINGS or other crap; // safeCall() is only a try-except struct to call in this case // str_replace('xx','yy', ...) and if it fails, // return the default instead of the actual result. $x = safeCall ( defaults('returnedByFuncB not a string'), 'str_replace', 'xx','yy',$returnedByFuncB ); if (is_string($returnedByFuncB) && is_string($returnedByFuncD)) { return goodResult ( $returnedByFuncB . $returnedByFuncD . $x ); // goodResult() and badResult() will both call // endErrorContext(), and copy the errors in // $callsErrors[$callID] // to the array returned to the calling function. // goodResult() and badResult() will call processErrors(), // which is repsonsible for mailing / db-storage of any // notices, warnings, errors, etc. } else { // badResult() will call getDebugInfo() to include // a full trace, superglobals, lotsa details. return badResult (array( 'errorMsg' => 'params 1 and 2 must be strings' 'onErrorDefault' => '' ));
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On Tue, 9 Feb 2010 16:09:05 +, rquadl...@googlemail.com (Richard Quadling) wrote: >On 9 February 2010 14:20, Ashley Sheridan wrote: >> >> On Tue, 2010-02-09 at 09:19 -0500, Robert Cummings wrote: >> >> Richard wrote: >> > Hi, >> > >> >> I have extended the standard exception class to send me an email >> >> whenever an exception occurs. >> > >> > I did that once. Once being the operative word... :-) Ended up with >> > tens of thousands of emails one morning. At first I thought... "Wow, >> > maybe my popularity has grown somewhat". But it hadn't. >> >> I have something similar... a cron job that checks the error_log file >> every 10 minutes and sends me the contents if any exist. I also set a >> special header so I can be sure it's not spam and appropriately route it >> into my mail folder maze, Much less spammy :) >> >> Cheers, >> Rob. >> -- >> http://www.interjinn.com >> Application and Templating Framework for PHP >> >> >> Real developers don't have errors in their code; they're undocumented >> features ;) >> >> Thanks, >> Ash >> http://www.ashleysheridan.co.uk >> >> > >So, no documentation AND bugs!!! Gee. I really wouldn't want to rely >on that code base! So you don't use (or work with) any Microsoft product? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
Hi, >> Real developers don't have errors in their code; they're undocumented >> features ;) Or alternatively, if you freelance - "Forthcoming employment opportunities" :-) -- Richard Heyes HTML5 canvas graphing: RGraph - http://www.rgraph.net (updated 7th February) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On 9 February 2010 14:20, Ashley Sheridan wrote: > > On Tue, 2010-02-09 at 09:19 -0500, Robert Cummings wrote: > > Richard wrote: > > Hi, > > > >> I have extended the standard exception class to send me an email > >> whenever an exception occurs. > > > > I did that once. Once being the operative word... :-) Ended up with > > tens of thousands of emails one morning. At first I thought... "Wow, > > maybe my popularity has grown somewhat". But it hadn't. > > I have something similar... a cron job that checks the error_log file > every 10 minutes and sends me the contents if any exist. I also set a > special header so I can be sure it's not spam and appropriately route it > into my mail folder maze, Much less spammy :) > > Cheers, > Rob. > -- > http://www.interjinn.com > Application and Templating Framework for PHP > > > Real developers don't have errors in their code; they're undocumented > features ;) > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > So, no documentation AND bugs!!! Gee. I really wouldn't want to rely on that code base! -- - 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
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
Ashley Sheridan wrote: On Tue, 2010-02-09 at 09:19 -0500, Robert Cummings wrote: Richard wrote: Hi, I have extended the standard exception class to send me an email whenever an exception occurs. I did that once. Once being the operative word... :-) Ended up with tens of thousands of emails one morning. At first I thought... "Wow, maybe my popularity has grown somewhat". But it hadn't. I have something similar... a cron job that checks the error_log file every 10 minutes and sends me the contents if any exist. I also set a special header so I can be sure it's not spam and appropriately route it into my mail folder maze, Much less spammy :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP Real developers don't have errors in their code; they're undocumented features ;) H... I've always gone with "a wise man learns from his mistakes". Not that I'm saying I'm wise or anything... just seems a good goal to shoot for :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On Tue, 2010-02-09 at 09:19 -0500, Robert Cummings wrote: > Richard wrote: > > Hi, > > > >> I have extended the standard exception class to send me an email > >> whenever an exception occurs. > > > > I did that once. Once being the operative word... :-) Ended up with > > tens of thousands of emails one morning. At first I thought... "Wow, > > maybe my popularity has grown somewhat". But it hadn't. > > I have something similar... a cron job that checks the error_log file > every 10 minutes and sends me the contents if any exist. I also set a > special header so I can be sure it's not spam and appropriately route it > into my mail folder maze, Much less spammy :) > > Cheers, > Rob. > -- > http://www.interjinn.com > Application and Templating Framework for PHP > Real developers don't have errors in their code; they're undocumented features ;) Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
Richard wrote: Hi, I have extended the standard exception class to send me an email whenever an exception occurs. I did that once. Once being the operative word... :-) Ended up with tens of thousands of emails one morning. At first I thought... "Wow, maybe my popularity has grown somewhat". But it hadn't. I have something similar... a cron job that checks the error_log file every 10 minutes and sends me the contents if any exist. I also set a special header so I can be sure it's not spam and appropriately route it into my mail folder maze, Much less spammy :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
Hi, > But I bet you REALLY quickly fixed the problem! I just took out the error handling. :-) -- Richard Heyes HTML5 canvas graphing: RGraph - http://www.rgraph.net (updated 7th February) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On 9 February 2010 12:55, Richard wrote: > Hi, > >> I have extended the standard exception class to send me an email >> whenever an exception occurs. > > I did that once. Once being the operative word... :-) Ended up with > tens of thousands of emails one morning. At first I thought... "Wow, > maybe my popularity has grown somewhat". But it hadn't. > > -- > Richard Heyes > HTML5 canvas graphing: RGraph - http://www.rgraph.net (updated 31st January) > Lots of PHP and Javascript code - http://www.phpguru.org > But I bet you REALLY quickly fixed the problem! Oh. BTW. Thanks for RMail (previously known as htmlmimemail5 if anyone else is interested). -- - 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
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
Hi, > I have extended the standard exception class to send me an email > whenever an exception occurs. I did that once. Once being the operative word... :-) Ended up with tens of thousands of emails one morning. At first I thought... "Wow, maybe my popularity has grown somewhat". But it hadn't. -- Richard Heyes HTML5 canvas graphing: RGraph - http://www.rgraph.net (updated 31st January) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On 9 February 2010 11:48, Nathan Rixham 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: > > > 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
Re: [PHP] Re: poll: howto do informative error handling without the fatalities
On Tue, 2010-02-09 at 07:22 +0100, Rene Veerman wrote: > I would also like to hear suggestions on how to fix this mess: > > $r = funcA ( funcB ( funcC ( $p ) ) ); > > if funcB() / funcC() fails, how would you fudge/abort the calling > function in the chain? > One may think that funcA and funcB just check their parameters list > for being "error" arrays, but the problem i foresee is that depending > on the context of the $r= call, desired behaviour may vary at any > stage in the funcA -> funcB -> funcC chain. > I would abort it by embedding the mess in a try.. catch statement, then throwing an exception. Teus. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php