Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I catch a "failed to decode JSON" error message in Perl?

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!!!!";
like image 421
Amey Avatar asked Oct 18 '11 15:10

Amey


People also ask

What is JSON decoding?

The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable. Syntax: json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 )

How do I read a JSON file in Perl script?

Decoding JSON in Perl (decode_json) Perl decode_json() function is used for decoding JSON in Perl. This function returns the value decoded from json to an appropriate Perl type.


1 Answers

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";
};
like image 107
Tamzin Blake Avatar answered Oct 05 '22 15:10

Tamzin Blake