我怎样才能捕获“无法解码 JSON”的错误? Perl 中的错误消息?

发布于 2024-12-10 17:19:02 字数 1651 浏览 0 评论 0原文

因此,我尝试加载测试返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2024-12-17 17:19:02

看起来您的错误消息来自 stderr,而不是 stdout。因此,

perl json_load_test.pl >> logs/output.txt 2>> logs/errors.txt

或者类似的东西。如果您希望将两者都放在一个文件中:

perl json_load_test.pl 2>&1 >> logs/output.txt

编辑:

如果出于某种原因您想在 perl 中捕获错误并将其发送到 stdout,您可以:

eval {
  decode_json($actual_response);
  1;
} or do {
  my $e = $@;
  print "$e\n";
};

编辑2:

作为 daxim在编辑注释中指出,Try::Tiny 可能更简单:

use Try::Tiny;
try {
  decode_json($actual_response);
} catch {
  print "$_\n";
};

It looks like your error message is coming from stderr, not stdout. Thus,

perl json_load_test.pl >> logs/output.txt 2>> logs/errors.txt

Or something to that effect. If you want both in one file:

perl json_load_test.pl 2>&1 >> logs/output.txt

EDIT:

If for some reason you wanted to trap the error in the perl and send it to stdout, you could:

eval {
  decode_json($actual_response);
  1;
} or do {
  my $e = $@;
  print "$e\n";
};

EDIT2:

As daxim points out in the edit notes, Try::Tiny may be simpler:

use Try::Tiny;
try {
  decode_json($actual_response);
} catch {
  print "$_\n";
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文