1

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);

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

Dumper Output:
$VAR1 = bless( {'vol' => '',
                'file_default' => 'file_default',
                '_error_msg' => 'Fetch failed! HTTP response: 502 Bad Gateway [502 notresolvable]',
                'file' => 'myfilename.zip',
                '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/Fetch.pm line 598.
John
  • 787
  • 4
  • 11
  • 28
  • So the script doesn't exit? Then you _are_ getting a warning? You can control it by localizing the `$SIG{__WARN__}`. – zdim Oct 18 '16 at 16:56

2 Answers2

0

It seems that the problem is a warning (message) being printed to STDERR. Apparently you are not getting a die or the program would exit. You can control the process of printing the message by setting the $SIG{__WARN__} hook, best localized in a block.

my $where;

FETCH: {

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

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

or

my $where = do { 
    local $SIG{__WARN__} = sub { print "WARN: @_" };
    $ff->fetch;
};

The signal's disposition – to print to STDERR – is restored outside of the block, which is what local provides. See this in perlsub, in particular text right after "Synopsis". 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.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • @duskwuff Thank you for the edit. I copied from docs and never noticed that (the ciritical) parts were unformatted. Thank you! – zdim Oct 19 '16 at 05:38
0

As the documentation explains, just set

$File::Fetch::WARN = 0;

to suppress warnings.

melpomene
  • 84,125
  • 8
  • 85
  • 148