The correct completion of the script

The question was asked: 6 years 9 months ago   views: 5
0

There is a parser in Perl. To upload pages using a library LWP::Simple.
When it comes to loading page when the unavailability of the resource, this error occurs:

Error GETing http://...: Status read failed: Connection reset by peer
As a result, the script aborts.

I would like to know how resolve this or how to handle this error?

Asked: angry angry 21-06-2012 в 16:54:26

Answers   3

0
eval {
 # Request here.
};
if ($@) {
 # Error handling here.
 # Error in $@.
}

Or

use Try::Tiny;

try {
 # Request here.
}
catch {
 # Error handling here.
 # Error in $_.
};
Answered: dionys dionys 21-06-2012 в 16:58:26
Thanks for the reply. It worked as it should. - Antonio Antonio 21-06-2012 в 18:19:34
this does not solve the problem. antonio, show your code in the place where the function is called get. - nörbörnën nörbörnën 21-06-2012 в 18:48:33
Here is the code of the method:
 sub get_page { my $self = shift; my $url = shift; print "URL:" . $url . "\n"; eval { $self->mechanize(@_)->get($url); }; if ($@){ print "ERROR GET PAGE\n"; $self->add_error('NOT_AVAILABLE', $@); return "; } if( exists$self->{CONFIG}->{page_timeout} ){ sleep($self->{_CONFIG}->{page_timeout}); } if( $self->mechanize(@)->success ){ return $self->mechanize(@_)->content(); } return "; } 
and it works fine now. - Antonio Antonio 21-06-2012 в 19:37:16
0

Use LWP::UserAgent

use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $response = $ua->get('http://....');
if ($response->is_success) {
 my $data = $response->content;
 # data processing
} else {
 # error handling
}
Answered: Dim_K Dim_K 21-06-2012 в 16:58:39
0

If you use the get function, it should return the value in case of a successful request and undef in case of failure. In other words:

my $response = get ( 'http://some.url/' );
if ( $response )
{
 # to process the information returned from the query
}
else 
{
 # action in case of unsuccessful response
}

PS: Excerpt from perldoc LWP::Simple specifically for cons:

The get() function will fetch the document identified by the given URL and return it. It returns "undef" if it fails.

In other words, if you use the type design

my $date = get ( $someUrl ) or die $!;

then skipt to die every time the query will severalise failure.

Answered: Кирилл Новгородцев Кирилл Новгородцев 21-06-2012 в 17:54:14