Skip to content

Commit

Permalink
Add Exception throw on error query response (#20)
Browse files Browse the repository at this point in the history
* add Exception throw on error query response

Signed-off-by: Alexey Chernyshov <[email protected]>

* fix PR issues

Signed-off-by: Alexey Chernyshov <[email protected]>

* Fix PR issues

Signed-off-by: Alexey Chernyshov <[email protected]>
  • Loading branch information
Alexey-N-Chernyshov authored and Warchant committed Apr 25, 2019
1 parent 97b4bfe commit 5b16f7f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jp.co.soramitsu.iroha.java;

import iroha.protocol.QryResponses.ErrorResponse;
import lombok.Getter;
import lombok.NonNull;

@Getter
public class ErrorResponseException extends RuntimeException {

@NonNull
private ErrorResponse errorResponse;

public ErrorResponseException(ErrorResponse errorResponse) {
super("Error code: " + errorResponse.getErrorCode() + ". Reason: " + errorResponse.getReason()
+ ". Message: " + errorResponse.getMessage());
this.errorResponse = errorResponse;
}
}
29 changes: 28 additions & 1 deletion client/src/main/java/jp/co/soramitsu/iroha/java/QueryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import iroha.protocol.QryResponses.AccountResponse;
import iroha.protocol.QryResponses.AssetResponse;
import iroha.protocol.QryResponses.BlockResponse;
import iroha.protocol.QryResponses.ErrorResponse;
import iroha.protocol.QryResponses.QueryResponse;
import iroha.protocol.QryResponses.TransactionsPageResponse;
import iroha.protocol.QryResponses.TransactionsResponse;
import java.security.KeyPair;
Expand Down Expand Up @@ -39,6 +41,13 @@ public QueryAPI(IrohaAPI api, Account account) {

private AtomicInteger counter = new AtomicInteger(1);

private void checkErrorResponse(QueryResponse response) {
if (response.hasErrorResponse()) {
ErrorResponse errorResponse = response.getErrorResponse();
throw new ErrorResponseException(errorResponse);
}
}

public String getAccountDetails(
String accountId,
String writer,
Expand All @@ -50,6 +59,8 @@ public String getAccountDetails(

val res = api.query(q);

checkErrorResponse(res);

val adr = res.getAccountDetailResponse();

return adr.getDetail();
Expand All @@ -62,6 +73,8 @@ public AccountResponse getAccount(String accountId) {

val res = api.query(q);

checkErrorResponse(res);

return res.getAccountResponse();
}

Expand All @@ -70,7 +83,11 @@ public BlockResponse getBlock(Long height) {
.getBlock(height)
.buildSigned(keyPair);

return api.query(q).getBlockResponse();
val res = api.query(q);

checkErrorResponse(res);

return res.getBlockResponse();
}

public TransactionsPageResponse getAccountTransactions(String accountId, Integer pageSize,
Expand All @@ -81,6 +98,8 @@ public TransactionsPageResponse getAccountTransactions(String accountId, Integer

val res = api.query(q);

checkErrorResponse(res);

return res.getTransactionsPageResponse();
}

Expand All @@ -96,6 +115,8 @@ public TransactionsPageResponse getAccountAssetTransactions(String accountId, St

val res = api.query(q);

checkErrorResponse(res);

return res.getTransactionsPageResponse();
}

Expand All @@ -119,6 +140,8 @@ public TransactionsResponse getTransactions(Iterable<String> hashes) {

val res = api.query(q);

checkErrorResponse(res);

return res.getTransactionsResponse();
}

Expand All @@ -129,6 +152,8 @@ public AssetResponse getAssetInfo(String assetId) {

val res = api.query(q);

checkErrorResponse(res);

return res.getAssetResponse();
}

Expand All @@ -139,6 +164,8 @@ public AccountAssetResponse getAccountAssets(String accountId) {

val res = api.query(q);

checkErrorResponse(res);

return res.getAccountAssetsResponse();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jp.co.soramitsu.iroha.java


import jp.co.soramitsu.iroha.java.debug.Account
import jp.co.soramitsu.iroha.testcontainers.IrohaContainer
import jp.co.soramitsu.iroha.testcontainers.PeerConfig
Expand Down Expand Up @@ -92,4 +91,37 @@ class QueryApiTest extends Specification {
A | B.id | A.id | "key2" | "{\"a@test\" : {\"key2\" : null}}"
}

@Unroll
def "exception in getAccountDetails"(Account issuer, String accountId, String writer, String key) {
given:
def qapi = new QueryAPI(api, issuer)

when:
qapi.getAccountDetails(accountId, writer, key)

then:
thrown ErrorResponseException

where:
issuer | accountId | writer | key
A | "nonexistent@domain" | null | null
}

@Unroll
def "exception in getAccount"(Account issuer, String accountId) {
given:
def qapi = new QueryAPI(api, issuer)

when:
qapi.getAccount(accountId)

then:
thrown ErrorResponseException

where:
issuer | accountId
A | "nonexistent@domain"
A | "invalid"
}

}

0 comments on commit 5b16f7f

Please sign in to comment.