Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with error handling...

Status
Not open for further replies.

unholyangl

Programmer
Feb 14, 2006
10
US
I'm trapping errors from a wrapper script like the following:
eval { load(); };

Then errors are read with the $@ variable

Now lets say we encounter a problem like the following...

$hsoc = new IO::Socket::INET (
LocalHost => $LocalHost,
LocalPort => $HostPort,
Proto => 'tcp',
Listen => 5,
# Reuse => 1,
) or log::do(0, "WARNING: Socket open failed on port \"$HostPort\"") && return;

Basicly, I wanna raise errors in places like the above, where abviously my log module is being called so that the parent wrapper script can intercept this the same way and then deal with it (ex: log it, compare against last time error happened, how many times it's happened, and how to execute because of this)

I'm sure it's very simple I'm just stumped and none of the books I've downloaded seem to tell me exactly how to do this, either that or I've missed it. Of course this isn't a necessity to what I'm designing, but I'd really like to know.

Thx in advance!! - UnholyAngl
 
it might be simple but I just do not understand what you are trying to do, then again, maybe it's not so simple :/
 
I'm capturing errors, logging, and restarting as needed.

Heres a small piece of my code... But may give u a better understanding of what I'm doing. (Wrapper script, loads other scripts with Eval)
Code:
our ($lasterr, $conterr)=(0,0);
do {
      logs::do(0, "Loading script: ".$cmd_path);
      eval { load(); }; # Trap Errors
      
      if ( $lasterr eq $@ ) { $conterr++; }
      else { $conterr=1; $lasterr = $@; }
      
      if ( $@ ) { logs::do(2, "Err (".$conterr."): ".$@); } # Log!
      else { logs::do(2, "Err (".$conterr."): No error reported!"); }
      
      if ( $conterr >= $MaxErrorRepeat && $MaxErrorRepeat ) {
         $NoSpawn = 1; # Prevents respawning script
         logs::do(2, "Error repeated ".$MaxErrorRepeat." times; Terminating Execution!");
      } else { sleep(1); }
      
} while !$NoSpawn;

Now heres another piece of code...
Code:
   my $hsoc = new IO::Socket::INET (
      LocalHost => $host,
      LocalPort => $port,
      Proto     => 'tcp',
      Listen    => $listen || 10,
      Reuse     => $reuse || 0,
   ) or logs::do(2, "Socket open failed on port \"$port\"".($host?" host \"$host\"":undef)) && return;
   logs::do(2, "Socket opened on port \"$port\"".($host?" host \"$host\"":undef));

and heres a screen shot:
log3af.jpg


See what I wanna accomplish? I want the error reported from the wrapper script, without some wierd hack/check. So what I wanna do, is raise an error as if Perl has done it. I tried setting $@, and that didn't work.

Any suggestions?
 
If you want Perl to raise an error/warning, why not use the 'warn' and 'die' functions?
 
because I want it passed to $@ so it can then be logged by the wrapper script...
 
Better yet, see how in the screen shot it's reporting as "No error reported!"? Well I wanna compare how many times and how often errors happen and execute based on the collected data (perhaps stop executing, log specific things, or just alot of ideas to aid in debugging the server environment)

if that makes any sense...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top