问题
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.
回答1:
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.
回答2:
As the documentation explains, just set
$File::Fetch::WARN = 0;
to suppress warnings.
来源:https://stackoverflow.com/questions/40112681/can-i-suppress-error-message-from-fetch-pm-in-perl