How to get a RPC error message in detail, more than "Internal server error"?

+1 vote

Hello,

 

I've installed Multichain and trying to make a Perl script to call JSON-RPC to a node, using JSON::RPC module.

 

    $client = JSON::RPC::Client->new;

    my $res = $client->call( $uri, $obj );
    if ( $res ) {
        if ($res->is_error) {
            # case 1
            $c->app->log->info( "Error : ". $res->error_message );
            return;
        }
        else {
            # case 2 - RPC success
            return $res->result;
        }
    } else {
        # case 3
        $c->app->log->info( "Fail to receive response : ". $client->status_line );
        return;
    }

 

 

When I tried an invalid call, for example,

- sendasset more qty than I have ( debug.log output was "SendMoney() : Insufficient funds" )

- fill qty param with string("100"), not numeric(100) in the json object ( debug.log output was just "mcapi: API request failure" )

 

I expected the server node would send such error_message like "Insufficient funds"(case 1 in the code). However, my call() just returned undef(case 3) and $client->status_line was just "500 Internal Server Error". So I could not get any useful hint from the client's log and I have to look into debug.log in the server node every time.

 

Could my client get this detailed message from server's response, by setting multichain daemon parameters?

 

Thank you.

 

asked Nov 10, 2016 by gypark

1 Answer

+1 vote
 
Best answer

Can you please tell us what you did to get this 500 error? It should not be returned for any API call, unless there is some internal bug.

answered Nov 11, 2016 by MultiChain
selected Nov 11, 2016 by gypark
Ooops. You are right. It's the bug of old Perl module. I found it by comparing with the result of cURL.

I've read https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#Perl and modify the script there. However, JSON::RPC::Client module might have been deprecated.

I changed to use JSON::RPC::Legacy::Client and its call() function now returns a valid json object which includes error code and message. Thank you!
Yes, JSON::RPC::Client doesn't handle error output from a node. However you can work around it by setting a LWP::UserAgent event handler:

      # We want to handle broken responses our self
      $client->ua->add_handler("response_data",
         sub {
            my ($response, $ua, $h, $data) = @_;

            if ($response->is_error) {
               my $content = JSON->new->utf8->decode($data);

               print STDERR "error code: ";
               print STDERR $content->{error}->{code} . "\n";
               print STDERR "error message:\n";
               print STDERR $content->{error}->{message};
            }

            return;
}

Also take a look at Bitcoin::RPC::Client, it wraps all of this up for you.
https://metacpan.org/pod/Bitcoin::RPC::Client
...