Bitcoin Forum
December 09, 2016, 09:35:04 PM *
News: Latest stable version of Bitcoin Core: 0.13.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: bitcoind & JSON-RPC PHP error handling problem  (Read 2806 times)
traced82
Newbie
*
Offline Offline

Activity: 20


View Profile
November 16, 2011, 07:54:03 PM
 #1

Hi there,
i use an dedicated bitcoind machine, and want to work with JSON-RPC PHP from Sergio Vaccaro.
Everything works fine, but if there is an error from bitcoind, like not enough balance to make a transaction,
JSON-RPC returns not the bitcoind error message, instead following error comes:

Warning: fopen(http://...@192.168.178.204:8332/) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in D:\xampp\htdocs\dev\bitcoin\jsonRPCClient.php on line 133

Is there a way to get the error message from bitcoind, to work with?
Or do you use another json-rpc library for such things?

thank you!!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481319304
Hero Member
*
Offline Offline

Posts: 1481319304

View Profile Personal Message (Offline)

Ignore
1481319304
Reply with quote  #2

1481319304
Report to moderator
Gavin Andresen
Legendary
*
Offline Offline

Activity: 1652


Chief Scientist


View Profile WWW
November 16, 2011, 10:43:14 PM
 #2

bitcoind follows the JSON-RPC-over-HTTP spec for reporting errors, but it sounds like Sergio's library follows a different spec. You're not the first person to complain that bitcoind returns HTTP error codes; here's a one-line patch to change that behavior with a "-rpcerrorstatus=200" argument/option:
Code:
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
index 31ef725..447b55c 100644
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -2088,6 +2088,9 @@ void ErrorReply(std::ostream& stream, const Object& objError, const Value& id)
     if (code == -32600) nStatus = 400;
     else if (code == -32601) nStatus = 404;
     string strReply = JSONRPCReply(Value::null, objError, id);
+
+    // Allow overriding the HTTP status response:
+    nStatus = GetArg("-rpcerrorstatus", nStatus);
     stream << HTTPReply(nStatus, strReply) << std::flush;
 }

Let me know if that solves the problem and/or if it causes any other issues; if it doesn't, I'll submit it as a PULL request.

How often do you get the chance to work on a potentially world-changing project?
traced82
Newbie
*
Offline Offline

Activity: 20


View Profile
November 17, 2011, 08:55:37 AM
 #3

wow, thanks for the quick fix. I will try to compile tonight, and let you know about it!

thx!!
traced82
Newbie
*
Offline Offline

Activity: 20


View Profile
November 17, 2011, 06:59:06 PM
 #4

Hi, just compiled bitcoind with your patch, and getting the following output:

The php code is:

Code:
try {
$test = $bitcoin->sendfrom('test_1', 'mn6zpXzT5ouQBjCXJe8Bbo8JZoxZA3d34y', 25, $minconf=1);
print $test;
} catch (Exception $e) {
    echo 'Exception abgefangen: ',  $e->getMessage(), "\n";

print '<pre>';
print_r($e);
print '</pre>';
}

And thats the output:

Code:
Exception abgefangen: Request error: Array

Exception Object
(
    [message:protected] => Request error: Array
    [string:Exception:private] =>
    [code:protected] => 0
    [file:protected] => D:\xampp\htdocs\dev\bitcoin\jsonRPCClient.php
    [line:protected] => 156
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => D:\xampp\htdocs\dev\bitcoin\test2.php
                    [line] => 28
                    [function] => __call
                    [class] => jsonRPCClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => sendfrom
                            [1] => Array
                                (
                                    [0] => test_1
                                    [1] => mn6zpXzT5ouQBjCXJe8Bbo8JZoxZA3d34y
                                    [2] => 25
                                    [3] => 1
                                )

                        )

                )

            [1] => Array
                (
                    [file] => D:\xampp\htdocs\dev\bitcoin\test2.php
                    [line] => 28
                    [function] => sendfrom
                    [class] => jsonRPCClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => test_1
                            [1] => mn6zpXzT5ouQBjCXJe8Bbo8JZoxZA3d34y
                            [2] => 25
                            [3] => 1
                        )

                )

        )

    [previous:Exception:private] =>
)

I don´t know if it is my fault because I misunderstood something here, but I can´t see any of the expected bitcoind error messages,
in this case it should throw an exception because the balance of the account is lower than the transaction amount.

thx!
traced82
Newbie
*
Offline Offline

Activity: 20


View Profile
November 17, 2011, 08:16:57 PM
 #5

As I am just at the beginning of developing a small application for me I´m not "fixed" to Sergio´s library.
Could you or the community suggest me any other json-rpc librarys for php that are robust and give me all possible functions?
theymos
Administrator
Legendary
*
Offline Offline

Activity: 2506


View Profile
November 21, 2011, 03:20:03 AM
 #6

Here's the code that Bitcoin Block Explorer uses for JSON-RPC communication:
http://pastebin.com/YCYb0nPv

You need to wrap each kind of use in a function like this that tests the output:
Code:
function getblockbynumber($num)
{
$data=rpcQuery("getblock",array($num));
if(!isset($data)||is_null($data)||is_null($data["r"])||!is_null($data["e"]))
{
//error handling...
}

return $data["r"];
}

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
traced82
Newbie
*
Offline Offline

Activity: 20


View Profile
November 29, 2011, 11:41:54 AM
 #7

hey, thanks for your reply and the code, and sorry for my late answer...
i will try this code out!

thx!!
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!