Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: CGI::Application cgi_prerun exception handling

by scorpio17 (Canon)
on Jan 04, 2011 at 20:10 UTC ( [id://880448]=note: print w/replies, xml ) Need Help??


in reply to CGI::Application cgi_prerun exception handling

You could do something like this:

sub cgiapp_prerun { my $this = shift; my $rm = shift; eval { ... }; if($@) { $this->stash->{exception} = "Error in prerun: $@"; $this->prerun_mode("error"); } } sub error { # error-runmode my $this = shift; my $exception = shift || $this->stash->{exception} || "default error message goes here"; ... }

Admittedly, this isn't that different... but it eliminates the prerun_error runmode. It also gives you the ability to call your error run mode either with an argument:

$this->error("danger! red alert!");

or without. If no argument is given, then the error run mode looks to see if there's anything in the stash, and if so it uses it - otherwise it spits out a generic default. You could use the stash to accumulate multiple errors. For example, if you were validating a form with lots of inputs, you could do something like:

# check 'name' entry unless ($name) { $this->stash->{exception} .= "bad name\n"; } # check 'address' entry unless ($address) { $this->stash->{exception} .= "bad address\n"; } # check 'phone' entry unless ($phone) { $this->stash->{exception} .= "bad phone\n"; } return $this->error( $this->stash->{exception} ) if $this->stash->{exception}; ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://880448]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-23 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found