我怎样才能捕获“无法解码 JSON”的错误? Perl 中的错误消息?
因此,我尝试加载测试返回 JSON 值的 REST API。
为此,我创建了 perl 脚本的多个实例。
Perl 脚本基本上会调用该 URL,并尝试decode_json
。显然,当产生大量负载时,它就会失败。
现在我面临的问题是 - 命令提示符上显示错误,但没有将该错误消息写入文件中。
错误消息是
格式错误的 JSON 字符串,不是数组、对象、数字、字符串或原子,位于 json_load_test.pl 第 39 行的字符偏移量 0(“无法连接到 209...”之前)。
在第 39 行以下的所有三个尝试中,
decode_json($actual_response);
我只是在命令提示符下运行脚本:
perl json_load_test.pl >> logs/output.txt
我希望将错误消息写入“output.txt”
我的三个失败尝试如下。
尝试 1:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
decode_json($actual_response);
if ($? == -1)
{print "\n Failed to execute: $!\n"; }
尝试 2:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
my $perl_scalar= decode_json($actual_response);
if ($perl_scalar)
{ok(1,"For process $u2 inside counter $counter ");}
else
{ok(0,"FAILED!!! process $u2 inside counter $counter");}
尝试 3:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
decode_json($actual_response) or die "FAILED!!!!";
So I am trying to load test a REST API which returns a JSON value.
To do that I am creating multiple instances of the perl script.
The Perl script basically calls that URL, and tries to decode_json
. Obviously when substantial load is generated, it fails.
Now the problem I face is- An error is displayed on command prompt but does not write that error message in a file.
The error message is
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "Can't connect to 209...") at json_load_test.pl line 39.
In all the three attempts below line 39 refers to:
decode_json($actual_response);
I am simply running the script on the command prompt as:
perl json_load_test.pl >> logs/output.txt
I EXPECT THE ERROR MESSAGE TO BE WRITTEN IN "output.txt"
My three failed attempts are as follows.
Attempt 1:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
decode_json($actual_response);
if ($? == -1)
{print "\n Failed to execute: $!\n"; }
Attempt 2:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
my $perl_scalar= decode_json($actual_response);
if ($perl_scalar)
{ok(1,"For process $u2 inside counter $counter ");}
else
{ok(0,"FAILED!!! process $u2 inside counter $counter");}
Attempt 3:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
decode_json($actual_response) or die "FAILED!!!!";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的错误消息来自 stderr,而不是 stdout。因此,
或者类似的东西。如果您希望将两者都放在一个文件中:
编辑:
如果出于某种原因您想在 perl 中捕获错误并将其发送到 stdout,您可以:
编辑2:
作为 daxim在编辑注释中指出,Try::Tiny 可能更简单:
It looks like your error message is coming from stderr, not stdout. Thus,
Or something to that effect. If you want both in one file:
EDIT:
If for some reason you wanted to trap the error in the perl and send it to stdout, you could:
EDIT2:
As daxim points out in the edit notes, Try::Tiny may be simpler: