Author

Topic: bitcoind & JSON-RPC PHP error handling problem (Read 3247 times)

newbie
Activity: 20
Merit: 0
November 29, 2011, 06: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!!
administrator
Activity: 5222
Merit: 13032
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"];
}
newbie
Activity: 20
Merit: 0
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?
newbie
Activity: 20
Merit: 0
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 '
';
print_r($e);
print '
';
}

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!
newbie
Activity: 20
Merit: 0
wow, thanks for the quick fix. I will try to compile tonight, and let you know about it!

thx!!
legendary
Activity: 1652
Merit: 2301
Chief Scientist
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.
newbie
Activity: 20
Merit: 0
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://[email protected]: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!!
Jump to: