John John - 11 months ago 44
Perl Question

Can i suppress error message from in Perl

When using Fetch to download a url from Teamcity I get a Fetch failed! error. But the download of the file actually works.

They have recently changed permissions of our Teamcity server so i've to use a username and password when obtaining the URL of the file to download. I'm just wondering if this is causing an issue with fetch's validation of the Gateway, but as I can download the file. Is there a way to suppress this error or just downgrade it to a warning?

Perl Code:
my $ff = File::Fetch->new(uri => "$uri");
my $where = $ff->fetch ( to => "$DOWNLOAD_LOCATION" );
print Dumper($ff);

Fetch failed! HTTP response: 502 Bad Gateway [502 notresolvable] at
<path>\ line 249.

Dumper Output:
$VAR1 = bless( {'vol' => '',
'file_default' => 'file_default',
'_error_msg' => 'Fetch failed! HTTP response: 502 Bad Gateway [502 notresolvable]',
'file' => '',
'scheme' => 'http',
'path' => '/repository/download/buildlabel/1042086:id/',
'_error_msg_long' => 'Fetch failed! HTTP response: 502 Bad Gateway [502 notresolvable] at C:/Perl/lib/File/ line 598.

Answer Source

It seems that your problem is a warning (message) being printed to STDERR. Apparently you are not getting a die or the program would exit. Then you can control the message and decide what should happen by localizing $SIG{__WARN__} in a block.

my $where;


    local $SIG{__WARN__} = sub { 
          say "WARN: @_";      # or whatever appropriate

    $where = $ff->fetch   ( to => "$DOWNLOAD_LOCATION" );    


my $where = do { 
    local $SIG{__WARN__} = sub { say "WARN: @_" };

The signal's disposition – to print to STDERR – is restored outside of the block. This is what local provides. See this in perlsub, in particular the very end of the linked section. You can also do that manually by saying $SIG{__WARN__} = 'DEFAULT'; once you are done.

See warn

No message is printed if there is a $SIG{__WARN__} handler installed. It is the handler's responsibility to deal with the message as it sees fit (like, for instance, converting it into a die).

Also see %SIG entry in perlvar

The routine indicated by $SIG{__WARN__} is called when a warning message is about to be printed. The warning message is passed as the first argument. The presence of a __WARN__ hook causes the ordinary printing of warnings to STDERR to be suppressed.

While deciding what to call an "error" and what a "warning" may be a bit arbitrary, it appears clear that your program only emits a message to STDERR and continues. Then the above should suffice.

If you were being hit by a die then you could wrap the code in eval.