我可以抑制Perl中来自fetch.pm的错误消息吗 [英] Can i suppress error message from fetch.pm in Perl
问题描述
使用Fetch从Teamcity下载url时,获取失败!错误.但是下载文件实际上是可行的.
When using Fetch to download a url from Teamcity I get a Fetch failed! error. But the download of the file actually works.
他们最近更改了Teamcity服务器的权限,因此在获取要下载文件的URL时,我必须使用用户名和密码.我只是想知道这是否导致网关的访存验证出现问题,但是我可以下载该文件.有没有办法抑制此错误或只是将其降级为警告?
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.
推荐答案
问题似乎出在将警告(消息)打印到STDERR
上.显然您没有获得die
,否则程序将退出.您可以通过设置$SIG{__WARN__}
钩子来控制打印消息的过程,该钩子最好定位在一个块中.
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" );
};
或
my $where = do {
local $SIG{__WARN__} = sub { print "WARN: @_" };
$ff->fetch;
};
信号的配置–打印到STDERR
–恢复到块外部,这是local
提供的.请参见此内容在perlsub中,尤其是文本就在"简介"之后.完成操作后,您也可以通过说$SIG{__WARN__} = 'DEFAULT';
手动完成操作.
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.
请参见警告
如果安装了
$SIG{__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).
另请参见%SIG
条目在perlvar中
当将要打印警告消息时,将调用由
$SIG{__WARN__}
指示的例程.警告消息作为第一个参数传递.__WARN__
钩子的存在会导致对STDERR的常规警告打印受到抑制.
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.
尽管确定什么是错误"以及什么为警告"可能有些武断,但显然您的程序仅向STDERR
发出消息并继续.那么上面的内容就足够了.
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.
如果您被die
击中,则可以将代码包装在 eval .
If you were being hit by a die
then you could wrap the code in eval.
这篇关于我可以抑制Perl中来自fetch.pm的错误消息吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!