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

后端 未结 1 709
一整个雨季
一整个雨季 2021-01-02 06:12

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.

相关标签:
1条回答
  • 2021-01-02 06:35

    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";
    };
    
    0 讨论(0)
提交回复
热议问题