Skip to content

Commit 4ce3507

Browse files
author
Luis Sanchez
committed
[FAB-3235] cleanup chaincode shim error handling
- only status codes >= 500 result in failed tx - Error repsonses only carry string messages - introduced non-proto Chaincode.Response - DRYed up Handler Change-Id: I3a458e0281ce60b633552ab35183b5be44ded6e6 Signed-off-by: Luis Sanchez <[email protected]>
1 parent 7eada3b commit 4ce3507

File tree

14 files changed

+364
-747
lines changed

14 files changed

+364
-747
lines changed

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/Chaincode.java

+63-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
// limitations under the License.
1414
package org.hyperledger.fabric.shim;
1515

16-
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
16+
import static java.nio.charset.StandardCharsets.UTF_8;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
1720

1821
/**
1922
* Defines methods that all chaincodes must implement.
@@ -30,4 +33,63 @@ public interface Chaincode {
3033
* variables.
3134
*/
3235
public Response invoke(ChaincodeStub stub);
36+
37+
public static class Response {
38+
39+
private final Status status;
40+
private final String message;
41+
private final byte[] payload;
42+
43+
public Response(Status status, String message, byte[] payload) {
44+
this.status = status;
45+
this.message = message;
46+
this.payload = payload;
47+
}
48+
49+
public Status getStatus() {
50+
return status;
51+
}
52+
53+
public String getMessage() {
54+
return message;
55+
}
56+
57+
public byte[] getPayload() {
58+
return payload;
59+
}
60+
61+
public String getStringPayload() {
62+
return new String(payload, UTF_8);
63+
}
64+
65+
public enum Status {
66+
SUCCESS(200),
67+
INTERNAL_SERVER_ERROR(500);
68+
69+
private static final Map<Integer, Status> codeToStatus = new HashMap<>();
70+
private final int code;
71+
72+
private Status(int code) {
73+
this.code = code;
74+
}
75+
76+
public int getCode() {
77+
return code;
78+
}
79+
80+
public static Status forCode(int code) {
81+
final Status result = codeToStatus.get(code);
82+
if(result == null) throw new IllegalArgumentException("no status for code " + code);
83+
return result;
84+
}
85+
86+
static {
87+
for (Status status : Status.values()) {
88+
codeToStatus.put(status.code, status);
89+
}
90+
}
91+
92+
}
93+
94+
}
3395
}

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java

+51-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
package org.hyperledger.fabric.shim;
1818

19+
import static org.hyperledger.fabric.shim.Chaincode.Response.Status.INTERNAL_SERVER_ERROR;
20+
import static org.hyperledger.fabric.shim.Chaincode.Response.Status.SUCCESS;
21+
1922
import java.io.File;
23+
import java.io.PrintWriter;
24+
import java.io.StringWriter;
25+
import java.nio.charset.StandardCharsets;
2026

2127
import javax.net.ssl.SSLException;
2228

@@ -30,7 +36,6 @@
3036
import org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type;
3137
import org.hyperledger.fabric.protos.peer.ChaincodeSupportGrpc;
3238
import org.hyperledger.fabric.protos.peer.ChaincodeSupportGrpc.ChaincodeSupportStub;
33-
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
3439
import org.hyperledger.fabric.shim.impl.Handler;
3540
import org.hyperledger.fabric.shim.impl.NextStateInfo;
3641

@@ -209,8 +214,8 @@ public void onCompleted() {
209214
handler = new Handler(requestObserver, this);
210215

211216
// Send the ChaincodeID during register.
212-
ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(id)// TODO
213-
// params.get("chaincode.id.name"))
217+
ChaincodeID chaincodeID = ChaincodeID.newBuilder()
218+
.setName(id)
214219
.build();
215220

216221
ChaincodeMessage payload = ChaincodeMessage.newBuilder()
@@ -242,6 +247,49 @@ public void onCompleted() {
242247
}
243248
}
244249

250+
protected static Response newSuccessResponse(String message, byte[] payload) {
251+
return new Response(SUCCESS, message, payload);
252+
}
253+
254+
protected static Response newSuccessResponse() {
255+
return newSuccessResponse(null, null);
256+
}
257+
258+
protected static Response newSuccessResponse(String message) {
259+
return newSuccessResponse(message, null);
260+
}
261+
262+
protected static Response newSuccessResponse(byte[] payload) {
263+
return newSuccessResponse(null, payload);
264+
}
265+
266+
protected static Response newErrorResponse(String message, byte[] payload) {
267+
return new Response(INTERNAL_SERVER_ERROR, message, payload);
268+
}
269+
270+
protected static Response newErrorResponse() {
271+
return newErrorResponse(null, null);
272+
}
273+
274+
protected static Response newErrorResponse(String message) {
275+
return newErrorResponse(message, null);
276+
}
277+
278+
protected static Response newErrorResponse(byte[] payload) {
279+
return newErrorResponse(null, payload);
280+
}
281+
282+
protected static Response newErrorResponse(Throwable throwable) {
283+
return newErrorResponse(throwable.getMessage(), printStackTrace(throwable));
284+
}
285+
286+
private static byte[] printStackTrace(Throwable throwable) {
287+
if (throwable == null) return null;
288+
final StringWriter buffer = new StringWriter();
289+
throwable.printStackTrace(new PrintWriter(buffer));
290+
return buffer.toString().getBytes(StandardCharsets.UTF_8);
291+
}
292+
245293
static String toJsonString(ChaincodeMessage message) {
246294
try {
247295
return JsonFormat.printer().print(message);

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeHelper.java

-172
This file was deleted.

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323

2424
import org.hyperledger.fabric.protos.peer.ChaincodeEventPackage.ChaincodeEvent;
25-
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
25+
import org.hyperledger.fabric.shim.Chaincode.Response;
2626
import org.hyperledger.fabric.shim.ledger.CompositeKey;
2727
import org.hyperledger.fabric.shim.ledger.KeyModification;
2828
import org.hyperledger.fabric.shim.ledger.KeyValue;

0 commit comments

Comments
 (0)