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

Hi, I am trying to capture the runtime error I am getting while using Search::Elasticsearch::Scroll, below is my code.
$hashprimary_index = $es->scroll_helper( index => $primary_index, search_type => 'scan' ); while ($result_pi = $hashprimary_index->next) { #getting error on this + line #do something }
I am getting below error and script fails.
[Missing] ** [http://localhost:9200]-[404] Not Found, called from sub +Search::Elasticsearch::Scroll::next at script/merge_v1.2.pl line 392. + With vars: {'request' => {'serialize' => 'std','ignore' => [],'metho +d' => 'GET','body' => 'c2Nhbjs1OzM2OTE6d1lvWW9YTHZRbVNzZjR3RkdtR3FPZz +szNjkyOndZb1lvWEx2UW1Tc2Y0d0ZHbUdxT2c7MzY5Mzp3WW9Zb1hMdlFtU3NmNHdGR21 +HcU9nOzM2OTU6d1lvWW9YTHZRbVNzZjR3RkdtR3FPZzszNjk0OndZb1lvWEx2UW1Tc2Y0 +d0ZHbUdxT2c7MTt0b3RhbF9oaXRzOjI2MDs=','qs' => {'scroll' => '1m'},'pat +h' => '/_search/scroll','mime_type' => 'application/json'},'status_co +de' => 404,'body' => {'hits' => {'hits' => [],'total' => 260,'max_sco +re' => '0'},'_shards' => {'total' => 5,'failures' => [{'index' => und +ef,'reason' => {'reason' => 'No search context found for id [3691]',' +type' => 'search_context_missing_exception'},'shard' => -1},{'shard' +=> -1,'index' => undef,'reason' => {'reason' => 'No search context fo +und for id [3692]','type' => 'search_context_missing_exception'}},{'i +ndex' => undef,'reason' => {'reason' => 'No search context found for +id [3693]','type' => 'search_context_missing_exception'},'shard' => - +1},{'index' => undef,'reason' => {'reason' => 'No search context foun +d for id [3695]','type' => 'search_context_missing_exception'},'shard +' => -1},{'shard' => -1,'index' => undef,'reason' => {'reason' => 'No + search context found for id [3694]','type' => 'search_context_missin +g_exception'}}],'failed' => 5,'successful' => 0},'took' => 2,'_scroll +_id' => 'c2NhbjswOzE7dG90YWxfaGl0czoyNjA7','timed_out' => bless( do{\ +(my $o = 0)}, 'JSON::PP::Boolean' )}}

I am trying to capture the error and in log and move on with the next loop, but unable to capture it, Eval don't work.

Thanks,

Replies are listed 'Best First'.
Re: Capture Elasticsearch Scroll Error
by 1nickt (Canon) on Oct 17, 2016 at 14:18 UTC

    Hi! Your question is not clear. Do you want to capture the error you show, so that your code can continue to run (maybe do something with the error message) ... or do you mean you want to fix what is causing the error?

    The way forward always starts with a minimal test.
      Hi, I updated the post and hope it's more clear, I am trying to capture the error in log and then move on with the next loop, I tried eval but it's not working.
Re: Capture Elasticsearch Scroll Error
by Erez (Priest) on Oct 18, 2016 at 06:51 UTC

    Hi,
    First off, you'll be better advised to place the error message inside <code></code> tags, as it's horribly hard to read, as well as breaking the entire page.

    As for your question, if you are looking into capturing the error, as in "catching" it, either look into eval, or perhaps something more familiar like Try::Tiny

    Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert

      Hi, Thanks for the suggestion, I updated the post. I tried eval, but that's not working, I will try Try::Tiny and see if that's working and update the same.
Re: Capture Elasticsearch Scroll Error
by 1nickt (Canon) on Oct 18, 2016 at 20:50 UTC

    Thanks for updating the post.

    You might like to look at Try::Tiny.

    It puts the error in $_.

    Here's a demo:

    $ perl -Mstrict -Mwarnings -MTry::Tiny -E ' sub my_func { my $num = shift; say "=" x 20; say "Num: $num"; try { say( 1 / $num ); } catch { say "Your error message here."; say "Original error:"; say ">" . $_ . "<"; }; # semicolon say "Not dead"; } my_func( $_ ) for ( 0, 1 ); say "=" x 20; '
    Output:
    ==================== Num: 0 Your error message here. Original error: >Illegal division by zero at -e line 7. < Not dead ==================== Num: 1 1 Not dead ====================

    Hope this helps

    The way forward always starts with a minimal test.