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

[email protected] (Rene Veerman)
Newsgroups php.general
Message-ID <[email protected]>
On Wed, Feb 10, 2010 at 3:04 AM, Paul M Foster <[email protected]> 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);
}
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.