There is a parser for pearl. To load pages I use the LWP::Simple library.
When the page is loaded, when the resource is unavailable, the following error occurs:

Error GETing http: // ...: Status read failed: The connection was dropped by the other side.
As a result, the script crashes.

I would like to know how to fix this problem or how to handle this error?

    3 answers 3

     eval { # Запрос тут. }; if ($@) { # Обработка ошибки тут. # Ошибка в $@. } 

    Or

     use Try::Tiny; try { # Запрос тут. } catch { # Обработка ошибки тут. # Ошибка в $_. }; 
    • Thanks for the answer. Everything worked as it should. - antonio
    • it does not solve the problem. antonio, show your code in the place where the get function is called. - nörbörnën
    • Here is the method code: <pre> 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 ''; } </ pre> and it works fine now. - antonio

    Use LWP::UserAgent

     use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $response = $ua->get('http://....'); if ($response->is_success) { my $data = $response->content; # обработка данных } else { # обработка ошибки } 

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

       my $response = get ( 'http://some.url/' ); if ( $response ) { # обработать информацию, возвращенную по запросу } else { # действия, в случае неудачного ответа } 

      PS: Excerpt from perldoc LWP :: Simple specifically for the negative:

      The get () function and the return it. It returns "undef" if it fails.

      In other words, if you use a type construct

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

      then the skipt will die every time the request fails.