Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Apache2. Passing data between handlers

by artemave (Beadle)
on Apr 24, 2007 at 21:17 UTC ( [id://611854]=perlquestion: print w/replies, xml ) Need Help??

artemave has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use $r->pnotes() to pass error message from a business handler to the special error one.
Here is the module where the error case is supposed to happen:
sub handler : method { my ($self, $r) = @_; ... $self->{r}->pnotes(error => 'Darn'); return Apache2::Const::SERVER_ERROR; } ... 1;
Now the special error handler:
sub handler { my $r = shift; $r->content_type('text/html'); $r->print($r->pnotes("error")); return Apache2::Const::OK; } 1;
And finally httpd.conf part to connect it all:
... ErrorDocument 500 /error ... PerlModule dir_browse::error <Location /error> SetHandler perl-script PerlResponseHandler dir_browse::error </Location>
The problem is that pnotes('error') does not keep its value when redirected to error handler (i tried to redirect explicitly using headers_out->set(Location => ...) and Apache2::Const::REDIRECT but the result was the same).
Now, I've read in pnotes doc that "The values get reset automatically at the end of each HTTP request". Does that mean, that in the above case the request is finished before getting to error handler? Or there is some other issue?

Thanks,
Artem.

Replies are listed 'Best First'.
Re: Apache2. Passing data between handlers
by gryphon (Abbot) on Apr 25, 2007 at 00:24 UTC

    Greetings artemave,

    I think the problem here is that there are two seperate HTTP requests. First, the user sends an HTTP request for a page that executes code out of your first handler and ends with a return of SERVER_ERROR. That's going to complete the HTTP request and redirect the browser to dir_browse::error. So anything in pnotes will not exist across the two requests.

    You could try a different approach, maybe. You might want to look into calling dir_browse::error in the first handler just before you return SERVER_ERROR. I don't know the rest of your app, but also, you may want to look at throwing exceptions.

    DISCLAIMER: Note that this is all from my tired head without any testing. I could be very wrong.

    gryphon
    Whitepages.com Development Manager (WDDC)
    code('Perl') || die;

      Hello, gryphon.

      "You might want to look into calling dir_browse::error in the first handler just before you return SERVER_ERROR"

      That means I don't need special error handler at all, do I?
      The top catch block will do all the work, right?

      That was the way until I thought it could look better. I mean, no need to worry about error handling in the main app, all the stuff will come up behind the scene.

      Either way, still an opened question here: is there a way to pass data between two handlers? Like if I ever need to build a chain of redirects or something.
      I'm pretty sure I'm missing something obvious after all. Or the whole idea is wrong and everybody does this and that, because it is so easy and obvious.

      Thanx,
      Artem.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-10-03 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (42 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.