Skip to content

Commit 5b16f7f

Browse files
Alexey-N-ChernyshovWarchant
authored andcommitted
Add Exception throw on error query response (#20)
* 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]>
1 parent 97b4bfe commit 5b16f7f

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package jp.co.soramitsu.iroha.java;
2+
3+
import iroha.protocol.QryResponses.ErrorResponse;
4+
import lombok.Getter;
5+
import lombok.NonNull;
6+
7+
@Getter
8+
public class ErrorResponseException extends RuntimeException {
9+
10+
@NonNull
11+
private ErrorResponse errorResponse;
12+
13+
public ErrorResponseException(ErrorResponse errorResponse) {
14+
super("Error code: " + errorResponse.getErrorCode() + ". Reason: " + errorResponse.getReason()
15+
+ ". Message: " + errorResponse.getMessage());
16+
this.errorResponse = errorResponse;
17+
}
18+
}

client/src/main/java/jp/co/soramitsu/iroha/java/QueryAPI.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import iroha.protocol.QryResponses.AccountResponse;
55
import iroha.protocol.QryResponses.AssetResponse;
66
import iroha.protocol.QryResponses.BlockResponse;
7+
import iroha.protocol.QryResponses.ErrorResponse;
8+
import iroha.protocol.QryResponses.QueryResponse;
79
import iroha.protocol.QryResponses.TransactionsPageResponse;
810
import iroha.protocol.QryResponses.TransactionsResponse;
911
import java.security.KeyPair;
@@ -39,6 +41,13 @@ public QueryAPI(IrohaAPI api, Account account) {
3941

4042
private AtomicInteger counter = new AtomicInteger(1);
4143

44+
private void checkErrorResponse(QueryResponse response) {
45+
if (response.hasErrorResponse()) {
46+
ErrorResponse errorResponse = response.getErrorResponse();
47+
throw new ErrorResponseException(errorResponse);
48+
}
49+
}
50+
4251
public String getAccountDetails(
4352
String accountId,
4453
String writer,
@@ -50,6 +59,8 @@ public String getAccountDetails(
5059

5160
val res = api.query(q);
5261

62+
checkErrorResponse(res);
63+
5364
val adr = res.getAccountDetailResponse();
5465

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

6374
val res = api.query(q);
6475

76+
checkErrorResponse(res);
77+
6578
return res.getAccountResponse();
6679
}
6780

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

73-
return api.query(q).getBlockResponse();
86+
val res = api.query(q);
87+
88+
checkErrorResponse(res);
89+
90+
return res.getBlockResponse();
7491
}
7592

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

8299
val res = api.query(q);
83100

101+
checkErrorResponse(res);
102+
84103
return res.getTransactionsPageResponse();
85104
}
86105

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

97116
val res = api.query(q);
98117

118+
checkErrorResponse(res);
119+
99120
return res.getTransactionsPageResponse();
100121
}
101122

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

120141
val res = api.query(q);
121142

143+
checkErrorResponse(res);
144+
122145
return res.getTransactionsResponse();
123146
}
124147

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

130153
val res = api.query(q);
131154

155+
checkErrorResponse(res);
156+
132157
return res.getAssetResponse();
133158
}
134159

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

140165
val res = api.query(q);
141166

167+
checkErrorResponse(res);
168+
142169
return res.getAccountAssetsResponse();
143170
}
144171
}

client/src/test/groovy/jp/co/soramitsu/iroha/java/QueryApiTest.groovy

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package jp.co.soramitsu.iroha.java
22

3-
43
import jp.co.soramitsu.iroha.java.debug.Account
54
import jp.co.soramitsu.iroha.testcontainers.IrohaContainer
65
import jp.co.soramitsu.iroha.testcontainers.PeerConfig
@@ -92,4 +91,37 @@ class QueryApiTest extends Specification {
9291
A | B.id | A.id | "key2" | "{\"a@test\" : {\"key2\" : null}}"
9392
}
9493

94+
@Unroll
95+
def "exception in getAccountDetails"(Account issuer, String accountId, String writer, String key) {
96+
given:
97+
def qapi = new QueryAPI(api, issuer)
98+
99+
when:
100+
qapi.getAccountDetails(accountId, writer, key)
101+
102+
then:
103+
thrown ErrorResponseException
104+
105+
where:
106+
issuer | accountId | writer | key
107+
A | "nonexistent@domain" | null | null
108+
}
109+
110+
@Unroll
111+
def "exception in getAccount"(Account issuer, String accountId) {
112+
given:
113+
def qapi = new QueryAPI(api, issuer)
114+
115+
when:
116+
qapi.getAccount(accountId)
117+
118+
then:
119+
thrown ErrorResponseException
120+
121+
where:
122+
issuer | accountId
123+
A | "nonexistent@domain"
124+
A | "invalid"
125+
}
126+
95127
}

0 commit comments

Comments
 (0)