Fix error codes from various RPCs #9853

pull jnewbery wants to merge 7 commits into bitcoin:master from jnewbery:fixerrorcodes changing 13 files +134 −140
  1. jnewbery commented at 4:28 pm on February 24, 2017: member

    (Completes #9707 and #9842)

    I’ve changed the qa test code to properly assert error codes and messages for failed RPC calls. That’s revealed a few RPCs which were returning incorrect or misleading error codes. This PR fixes all of those cases.

    It also adds commenting around the definition of the JSON RPC error constants to warn against using RPC_INVALID_REQUEST and RPC_METHOD_NOT_FOUND for application-layer errors.

  2. in qa/rpc-tests/bumpfee.py: in a24d00cb9d outdated
    127@@ -128,7 +128,7 @@ def test_segwit_bumpfee_succeeds(rbf_node, dest_address):
    128 def test_nonrbf_bumpfee_fails(peer_node, dest_address):
    129     # cannot replace a non RBF transaction (from node which did not enable RBF)
    130     not_rbfid = create_fund_sign_send(peer_node, {dest_address: 0.00090000})
    131-    assert_raises_message(JSONRPCException, "not BIP 125 replaceable", peer_node.bumpfee, not_rbfid)
    132+    assert_raises_jsonrpc(-4, "not BIP 125 replaceable", peer_node.bumpfee, not_rbfid)
    


    ryanofsky commented at 4:35 pm on February 24, 2017:
    Is there a plan to switch from integer literals to constants in a later PR? Seems like it would be nice to say RPC_WALLET_ERROR instead of -4.

    laanwj commented at 9:31 am on February 25, 2017:
    Yes that would be nice, but is orthogonal to this change, so if you want to do that it’d indeed be a different PR.

    jnewbery commented at 10:30 pm on February 28, 2017:
    yep, I’ll put that in the todo list. I’m working on a primitives.py for bitcoin messages, datastructures and constants. Seems like a good place for the RPC error constants to live.
  3. fanquake added the label RPC/REST/ZMQ on Feb 25, 2017
  4. in src/wallet/rpcwallet.cpp: in a24d00cb9d outdated
    2752@@ -2753,33 +2753,33 @@ UniValue bumpfee(const JSONRPCRequest& request)
    2753     CWalletTx& wtx = pwalletMain->mapWallet[hash];
    2754 
    2755     if (pwalletMain->HasWalletSpend(hash)) {
    2756-        throw JSONRPCError(RPC_MISC_ERROR, "Transaction has descendants in the wallet");
    2757+        throw JSONRPCError(RPC_WALLET_ERROR, "Transaction has descendants in the wallet");
    


    ryanofsky commented at 7:21 pm on February 28, 2017:

    In cases where bumpfee is called erroneously with a transaction that shouldn’t be bumped, invalid parameter seems more apt than wallet error.

    Probably the most important thing you want to know when you get an error message from an API is whether the error is caused by something that you control and need to fix, or whether it’s caused by something out of your control (like an implementation bug, or corrupt data, or a temporary failure). RPC_INVALID_PARAMETER clearly tells you that you need to fix something, while RPC_WALLET_ERROR is vague but suggests that something’s wrong with the wallet.


    jnewbery commented at 10:31 pm on February 28, 2017:

    Probably the most important thing you want to know when you get an error message from an API is whether the error is caused by something that you control and need to fix, or whether it’s caused by something out of your control

    Nicely put. I’ll update this to RPC_INVALID_PARAMETER

  5. in src/wallet/rpcdump.cpp: in 4befdf5b64 outdated
    342+        throw JSONRPCError(RPC_WALLET_ERROR, "Could not properly delete the transaction.");
    343     }
    344 
    345     if(vHashOut.empty()) {
    346-        throw JSONRPCError(RPC_INTERNAL_ERROR, "Transaction does not exist in wallet.");
    347+        throw JSONRPCError(RPC_WALLET_ERROR, "Transaction does not exist in wallet.");
    


    ryanofsky commented at 7:29 pm on February 28, 2017:
    Maybe invalid parameter here as well.

    jnewbery commented at 10:33 pm on February 28, 2017:
    ACK
  6. in src/wallet/rpcdump.cpp: in 4befdf5b64 outdated
    337@@ -338,11 +338,11 @@ UniValue removeprunedfunds(const JSONRPCRequest& request)
    338     vector<uint256> vHashOut;
    339 
    340     if(pwalletMain->ZapSelectTx(vHash, vHashOut) != DB_LOAD_OK) {
    341-        throw JSONRPCError(RPC_INTERNAL_ERROR, "Could not properly delete the transaction.");
    342+        throw JSONRPCError(RPC_WALLET_ERROR, "Could not properly delete the transaction.");
    


    ryanofsky commented at 7:47 pm on February 28, 2017:
    ZapSelect returns a DBErrors enum value which seems like it could be mapped to an RPC error (would also be nice to include in the error string).

    jnewbery commented at 10:32 pm on February 28, 2017:

    I agree we could do better, but let’s leave that for a future PR.

    I think this is strictly an improvement because currently the API is returning RPC_INTERNAL_ERROR even in cases where there’s no internal error.

  7. in src/rpc/blockchain.cpp: in 119d4e8d5d outdated
    833@@ -829,7 +834,7 @@ UniValue pruneblockchain(const JSONRPCRequest& request)
    834             + HelpExampleRpc("pruneblockchain", "1000"));
    835 
    836     if (!fPruneMode)
    837-        throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Cannot prune blocks because node is not in prune mode.");
    838+        throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode.");
    


    ryanofsky commented at 7:50 pm on February 28, 2017:
    Some of these MISC_ERROR cases make it seem like we could benefit from an new RPC_INVALID_STATE value. (Blockchain too short is another case below.)

    jnewbery commented at 10:33 pm on February 28, 2017:
    Sounds sensible, but again let’s leave that for a future PR.

    laanwj commented at 10:45 am on March 2, 2017:
    Yes, there is no reason why we couldn’t add new status values. There’s no need to make everything fit in the arbitrary bag of codes that exists now, which has been there unchanged for a long while.
  8. ryanofsky commented at 7:56 pm on February 28, 2017: member
    utACK b34faf156d49705faa335641cfbfc0efd60bb781, definitely an improvement.
  9. jnewbery force-pushed on Mar 1, 2017
  10. Return correct error codes in bumpfee().
    The bumpfee() RPC was returning misleading or incorrect error codes
    (for example RPC_INVALID_ADDRESS_OR_KEY when the transaction was not
    BIP125 replacable). This commit fixes those error codes:
    
    - RPC_INVALID_ADDRESS_OR_KEY if an invalid address was provided:
        - Invalid change address given
    - RPC_INVALID_PARAMETER if a single (non-address/key) parameter is incorrect
        - confTarget and totalFee options should not both be set.
        - Invalid confTarget
        - Insufficient totalFee (cannot be less than required fee)
    - RPC_WALLET_ERROR for any other error
        - Transaction has descendants in the wallet
        - Transaction has descendants in the mempool
        - Transaction has been mined, or is conflicted with a mined transaction
        - Transaction is not BIP 125 replaceable
        - Transaction has already been bumped
        - Transaction contains inputs that don't belong to the wallet
        - Transaction has multiple change outputs
        - Transaction does not have a change output
        - Fee is higher than maxTxFee
        - New fee rate is less than the minimum fee rate
        - Change output is too small.
    
    This commit also updates the test cases to explicitly test the error code.
    6d07c62322
  11. Return correct error codes in blockchain.cpp.
    RPCs in blockchain.cpp were returning misleading or incorrect error
    codes (for example getblock() returning RPC_INTERNAL_ERROR when the
    block had been pruned). This commit fixes those error codes:
    
    - RPC_INTERNAL_ERROR should not be returned for application-level
      errors, only for genuine internal errors such as corrupted data.
    - RPC_METHOD_NOT_FOUND should not be returned in response to a
      JSON request for an existing method.
    
    Those error codes have been replaced with RPC_MISC_ERROR or
    RPC_INVALID_PARAMETER as appropriate.
    c1190963b3
  12. Return correct error codes in removeprunedfunds().
    The removeprunedfunds() RPC was returning misleading or incorrect error
    codes (for example RPC_INTERNAL_ERROR when the transaction was
    not found in the wallet). This commit fixes those error codes:
    
    - RPC_INTERNAL_ERROR should not be returned for application-level
    errors, only for genuine internal errors such as corrupted data.
    
    This error code has been replaced with RPC_WALLET_ERROR.
    
    This commit also updates the test cases to explicitly test the error code.
    960bc7f778
  13. Return correct error codes in setban().
    The setban() RPC was returning misleading or incorrect error
    codes (for example RPC_CLIENT_NODE_ALREADY_ADDED when an invalid IP
    address was entered). This commit fixes those error codes:
    
    - RPC_CLIENT_INVALID_IP_OR_SUBNET should be returned if the client
      enters an invalid IP address or subnet.
    
    This commit also updates the test cases to explicitly test the error code.
    
    This commit also adds a testcase for trying to setban on an invalid subnet.
    a012087667
  14. Return correct error codes in fundrawtransaction().
    The fundrawtransaction() RPC was returning misleading or incorrect error
    codes (for example RPC_INTERNAL_ERROR when funding the transaction
    failed). This commit fixes those error codes:
    
    - RPC_INTERNAL_ERROR should not be returned for application-level
    errors, only for genuine internal errors such as corrupted data.
    
    That error code has been replaced with RPC_WALLET_ERROR.
    
    This commit also updates the test cases to explicitly test the error code.
    dab804c18a
  15. jnewbery force-pushed on Mar 7, 2017
  16. laanwj assigned laanwj on Mar 8, 2017
  17. laanwj commented at 2:51 pm on March 8, 2017: member
    utACK 3ff485e
  18. MarcoFalke commented at 3:07 pm on March 8, 2017: member

    Concept ACK.

    Style Nit: In blockchain.cpp you are missing braces around the multiline if block

    Also, I was wondering if this requires a mention in the release notes…

  19. laanwj commented at 3:15 pm on March 8, 2017: member

    Also, I was wondering if this requires a mention in the release notes…

    It does.

  20. ryanofsky commented at 6:34 pm on March 8, 2017: member

    utACK 3ff485e32f7a1d068e799d323d3531ede22873f1

    Confirmed only minor changes since previous ACK.

  21. Add commenting around JSON error codes
    RPC_INVALID_REQUEST and RPC_METHOD_NOT_FOUND are mapped internally to
    HTTP error codes and should not be used for application-layer errors.
    This commit adds commenting around those definitions to warn not to use
    them for application errors.
    338bf065a4
  22. Update release notes to include RPC error code changes. adaa281da1
  23. jnewbery force-pushed on Mar 8, 2017
  24. jnewbery commented at 7:24 pm on March 8, 2017: member
    Release notes updated. Otherwise no change.
  25. laanwj merged this on Mar 9, 2017
  26. laanwj closed this on Mar 9, 2017

  27. laanwj referenced this in commit 02bd6e9bc6 on Mar 9, 2017
  28. jnewbery deleted the branch on Mar 9, 2017
  29. luke-jr referenced this in commit db650e58bd on Jun 3, 2017
  30. luke-jr referenced this in commit 783b4b5ab4 on Jun 3, 2017
  31. luke-jr referenced this in commit c72ef66488 on Jun 3, 2017
  32. luke-jr referenced this in commit 24909cb2e1 on Jun 3, 2017
  33. luke-jr referenced this in commit abf2414490 on Jun 3, 2017
  34. luke-jr referenced this in commit 0fb9bab956 on Jun 3, 2017
  35. luke-jr referenced this in commit efa7de664d on Jun 3, 2017
  36. luke-jr referenced this in commit 6b6d912496 on Jun 3, 2017
  37. luke-jr referenced this in commit 1813a4a171 on Jun 3, 2017
  38. luke-jr referenced this in commit fa897c1f93 on Jun 3, 2017
  39. luke-jr referenced this in commit 50b052d71b on Jun 3, 2017
  40. luke-jr referenced this in commit a44f6848fe on Jun 3, 2017
  41. luke-jr referenced this in commit 3ad00b4b32 on Jun 5, 2017
  42. luke-jr referenced this in commit fe51c8924e on Jun 5, 2017
  43. luke-jr referenced this in commit 18c109ddb1 on Jun 5, 2017
  44. luke-jr referenced this in commit 4943d7a9fe on Jun 5, 2017
  45. luke-jr referenced this in commit f5efe82a83 on Jun 5, 2017
  46. luke-jr referenced this in commit c25d0a8739 on Jun 5, 2017
  47. luke-jr referenced this in commit cd10719178 on Jun 5, 2017
  48. luke-jr referenced this in commit 99e5dbd0aa on Jun 5, 2017
  49. luke-jr referenced this in commit 28734848dd on Jun 5, 2017
  50. luke-jr referenced this in commit e05799a381 on Jun 5, 2017
  51. nomnombtc referenced this in commit f81e9f7144 on Jul 17, 2017
  52. nomnombtc referenced this in commit 233996c67e on Jul 17, 2017
  53. nomnombtc referenced this in commit ae73a04f71 on Jul 17, 2017
  54. nomnombtc referenced this in commit 0f1741ba3c on Jul 17, 2017
  55. nomnombtc referenced this in commit fad19c6d33 on Jul 17, 2017
  56. nomnombtc referenced this in commit fc1aa80133 on Jul 17, 2017
  57. PastaPastaPasta referenced this in commit 1563115c7e on Jan 2, 2019
  58. PastaPastaPasta referenced this in commit 1a781e9d09 on Jan 2, 2019
  59. PastaPastaPasta referenced this in commit 320487b04f on Jan 2, 2019
  60. PastaPastaPasta referenced this in commit 02f6719d75 on Jan 3, 2019
  61. PastaPastaPasta referenced this in commit 4ffc299010 on Jan 21, 2019
  62. PastaPastaPasta referenced this in commit b479cddbbf on Jan 27, 2019
  63. PastaPastaPasta referenced this in commit 519cf9ae15 on Jan 29, 2019
  64. PastaPastaPasta referenced this in commit 39514f7c41 on Feb 5, 2019
  65. PastaPastaPasta referenced this in commit 18450227c2 on Feb 5, 2019
  66. PastaPastaPasta referenced this in commit 1be5a72a97 on Feb 5, 2019
  67. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-19 00:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me