Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: traced82 on November 16, 2011, 07:54:03 PM



Title: bitcoind & JSON-RPC PHP error handling problem
Post by: traced82 on November 16, 2011, 07:54:03 PM
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!!


Title: Re: bitcoind & JSON-RPC PHP error handling problem
Post by: Gavin Andresen on November 16, 2011, 10:43:14 PM
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.


Title: Re: bitcoind & JSON-RPC PHP error handling problem
Post by: traced82 on November 17, 2011, 08:55:37 AM
wow, thanks for the quick fix. I will try to compile tonight, and let you know about it!

thx!!


Title: Re: bitcoind & JSON-RPC PHP error handling problem
Post by: traced82 on November 17, 2011, 06:59:06 PM
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!


Title: Re: bitcoind & JSON-RPC PHP error handling problem
Post by: traced82 on November 17, 2011, 08:16:57 PM
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?


Title: Re: bitcoind & JSON-RPC PHP error handling problem
Post by: theymos on November 21, 2011, 03:20:03 AM
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"];
}


Title: Re: bitcoind & JSON-RPC PHP error handling problem
Post by: traced82 on November 29, 2011, 11:41:54 AM
hey, thanks for your reply and the code, and sorry for my late answer...
i will try this code out!

thx!!