Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: Catching time-outs

by coldfingertips (Pilgrim)
on Mar 29, 2004 at 17:01 UTC ( #340677=note: print w/replies, xml ) Need Help??


in reply to Re: Catching time-outs
in thread Catching time-outs

I've read that already and I know how to setup a custom timeout. My question is, how can I trap it so IF it times out MY error message will be displayed? It doesn't show you that.

Replies are listed 'Best First'.
Re: Re: Re: Catching time-outs
by davido (Cardinal) on Mar 29, 2004 at 17:18 UTC
    Did you also read the part of the docs that says the return value of the get() method is a response object, and to see HTTP::Response for details?

    In HTTP::Response, there is an is_success() and an is_error() method. And again we're directed somewhere else: see HTTP::Status for the meaning of these methods...

    RC_REQUEST_TIMEOUT is one of the possible response codes.


    Dave

      I tried using this and it's not working. I set a very small timeout hoping it'd catch everything (just for a test) and some still took over 20 seconds and didn't say it timed out.
      use LWP::Simple qw(!head); use LWP::UserAgent; use HTML::LinkExtor; use URI::URL; my $ua = LWP::UserAgent->new; my $p = HTML::LinkExtor->new; $ua->timeout(3); my $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->parse($_[0])}); foreach(@url) { $url = "$_"; my $req = HTTP::Request->new(POST => "$url"); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist'); my $res = $ua->request($req); if ($res->status_line =~ /404/ || $res->status_line =~ /401/) { print "FAILED!"; } elsif ($res->status_line =~ /408/) { print "TIMED OUT"; } else { print "PASSED!!" } }

        I see what the problem is now after posting your code. LWP returns a status of 500 if a timeout occurs, not the 408 you are looking for.

        To catch your errors, you could try

        if ($res->success) { print "PASSED!\n"; } else { print "An error occured: " . $res->status_line . "\n"; }
      No, I didn't see that which leads to one final question. That is HTTP::Status, I am using LWP Simple and UserAgent. Does LWP::Simple use HTTP::Status, is this how it checks for error codes? If not, I don't see how I could use a UA-TIMEOUT and have HTTP::Status catch it.

      Thanks for your help!! This is very promising!

Re: Re: Re: Catching time-outs
by Vautrin (Hermit) on Mar 29, 2004 at 17:31 UTC

    If you die() or exit () in an eval block, the error goes to $! (or is it $@?) and the eval returns false. It's like Java's try/catch. If you die on not having success you can test for that. If, on the other hand, you mean that LWP::UserAgent is printing to STDERR, and you want to stop it, just redirect STDERR -- i.e. open ("STDERR", "> ./error").


    Want to support the EFF and FSF by buying cool stuff? Click here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2022-05-24 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?