[PAN-2705] helpful graphql error when an account doesn't exist (#1460)

When we are asking for an account at an address that does not exist the
error message should state that the account doesn't exist.
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Danno Ferrin 6 years ago committed by GitHub
parent 659c79b6d8
commit 0b53195b0b
  1. 22
      ethereum/graphqlrpc/src/main/java/tech/pegasys/pantheon/ethereum/graphqlrpc/GraphQLDataFetchers.java
  2. 2
      ethereum/graphqlrpc/src/test/java/tech/pegasys/pantheon/ethereum/graphqlrpc/EthGraphQLRpcHttpBySpecTest.java
  3. 22
      ethereum/graphqlrpc/src/test/resources/tech/pegasys/pantheon/ethereum/graphqlrpc/eth_getBalance_invalidAccountBlockNumber.json
  4. 22
      ethereum/graphqlrpc/src/test/resources/tech/pegasys/pantheon/ethereum/graphqlrpc/eth_getBalance_invalidAccountLatest.json

@ -12,7 +12,10 @@
*/
package tech.pegasys.pantheon.ethereum.graphqlrpc;
import static com.google.common.base.Preconditions.checkArgument;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.SyncStatus;
@ -163,7 +166,9 @@ public class GraphQLDataFetchers {
if (bn != null) {
final Optional<WorldState> ws = blockchainQuery.getWorldState(bn);
if (ws.isPresent()) {
return Optional.of(new AccountAdapter(ws.get().get(addr)));
final Account account = ws.get().get(addr);
checkArgument(account != null, "Account with address %s does not exist", addr);
return Optional.of(new AccountAdapter(account));
} else if (bn > blockchainQuery.getBlockchain().getChainHeadBlockNumber()) {
// block is past chainhead
throw new GraphQLRpcException(GraphQLRpcError.INVALID_PARAMS);
@ -171,11 +176,18 @@ public class GraphQLDataFetchers {
// we don't have that block
throw new GraphQLRpcException(GraphQLRpcError.CHAIN_HEAD_WORLD_STATE_NOT_AVAILABLE);
}
} else {
// return account on latest block
final long latestBn = blockchainQuery.latestBlock().get().getHeader().getNumber();
final Optional<WorldState> ows = blockchainQuery.getWorldState(latestBn);
return ows.flatMap(
ws -> {
Account account = ws.get(addr);
checkArgument(account != null, "Account with address %s does not exist", addr);
return Optional.ofNullable(account);
})
.map(AccountAdapter::new);
}
// return account on latest block
final long latestBn = blockchainQuery.latestBlock().get().getHeader().getNumber();
final Optional<WorldState> ows = blockchainQuery.getWorldState(latestBn);
return ows.flatMap(ws -> Optional.ofNullable(ws.get(addr))).map(AccountAdapter::new);
};
}

@ -72,6 +72,8 @@ public class EthGraphQLRpcHttpBySpecTest extends AbstractEthGraphQLRpcHttpServic
specs.add("eth_call_BlockLatest");
specs.add("eth_getBalance_latest");
specs.add("eth_getBalance_0x19");
specs.add("eth_getBalance_invalidAccountBlockNumber");
specs.add("eth_getBalance_invalidAccountLatest");
specs.add("eth_gasPrice");
specs.add("eth_getTransactionReceipt");

@ -0,0 +1,22 @@
{
"request": "{account(blockNumber:\"0x19\", address: \"0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef\") { balance } }",
"response": {
"data": null,
"errors": [
{
"message": "Exception while fetching data (/account) : Account with address 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef does not exist",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"account"
]
}
]
},
"statusCode": 400
}

@ -0,0 +1,22 @@
{
"request": "{account(address: \"0xdeaff00ddeaff00ddeaff00ddeaff00ddeaff00d\") { balance } }",
"response": {
"data": null,
"errors": [
{
"message": "Exception while fetching data (/account) : Account with address 0xdeaff00ddeaff00ddeaff00ddeaff00ddeaff00d does not exist",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"account"
]
}
]
},
"statusCode": 400
}
Loading…
Cancel
Save