Skip to content

Commit

Permalink
[FAB-3235] cleanup chaincode shim error handling
Browse files Browse the repository at this point in the history
- 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 <sanchezl@us.ibm.com>
  • Loading branch information
Luis Sanchez committed May 10, 2017
1 parent 7eada3b commit 4ce3507
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 747 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// limitations under the License.
package org.hyperledger.fabric.shim;

import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.util.HashMap;
import java.util.Map;

/**
* Defines methods that all chaincodes must implement.
Expand All @@ -30,4 +33,63 @@ public interface Chaincode {
* variables.
*/
public Response invoke(ChaincodeStub stub);

public static class Response {

private final Status status;
private final String message;
private final byte[] payload;

public Response(Status status, String message, byte[] payload) {
this.status = status;
this.message = message;
this.payload = payload;
}

public Status getStatus() {
return status;
}

public String getMessage() {
return message;
}

public byte[] getPayload() {
return payload;
}

public String getStringPayload() {
return new String(payload, UTF_8);
}

public enum Status {
SUCCESS(200),
INTERNAL_SERVER_ERROR(500);

private static final Map<Integer, Status> codeToStatus = new HashMap<>();
private final int code;

private Status(int code) {
this.code = code;
}

public int getCode() {
return code;
}

public static Status forCode(int code) {
final Status result = codeToStatus.get(code);
if(result == null) throw new IllegalArgumentException("no status for code " + code);
return result;
}

static {
for (Status status : Status.values()) {
codeToStatus.put(status.code, status);
}
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@

package org.hyperledger.fabric.shim;

import static org.hyperledger.fabric.shim.Chaincode.Response.Status.INTERNAL_SERVER_ERROR;
import static org.hyperledger.fabric.shim.Chaincode.Response.Status.SUCCESS;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

import javax.net.ssl.SSLException;

Expand All @@ -30,7 +36,6 @@
import org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type;
import org.hyperledger.fabric.protos.peer.ChaincodeSupportGrpc;
import org.hyperledger.fabric.protos.peer.ChaincodeSupportGrpc.ChaincodeSupportStub;
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
import org.hyperledger.fabric.shim.impl.Handler;
import org.hyperledger.fabric.shim.impl.NextStateInfo;

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

// Send the ChaincodeID during register.
ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(id)// TODO
// params.get("chaincode.id.name"))
ChaincodeID chaincodeID = ChaincodeID.newBuilder()
.setName(id)
.build();

ChaincodeMessage payload = ChaincodeMessage.newBuilder()
Expand Down Expand Up @@ -242,6 +247,49 @@ public void onCompleted() {
}
}

protected static Response newSuccessResponse(String message, byte[] payload) {
return new Response(SUCCESS, message, payload);
}

protected static Response newSuccessResponse() {
return newSuccessResponse(null, null);
}

protected static Response newSuccessResponse(String message) {
return newSuccessResponse(message, null);
}

protected static Response newSuccessResponse(byte[] payload) {
return newSuccessResponse(null, payload);
}

protected static Response newErrorResponse(String message, byte[] payload) {
return new Response(INTERNAL_SERVER_ERROR, message, payload);
}

protected static Response newErrorResponse() {
return newErrorResponse(null, null);
}

protected static Response newErrorResponse(String message) {
return newErrorResponse(message, null);
}

protected static Response newErrorResponse(byte[] payload) {
return newErrorResponse(null, payload);
}

protected static Response newErrorResponse(Throwable throwable) {
return newErrorResponse(throwable.getMessage(), printStackTrace(throwable));
}

private static byte[] printStackTrace(Throwable throwable) {
if (throwable == null) return null;
final StringWriter buffer = new StringWriter();
throwable.printStackTrace(new PrintWriter(buffer));
return buffer.toString().getBytes(StandardCharsets.UTF_8);
}

static String toJsonString(ChaincodeMessage message) {
try {
return JsonFormat.printer().print(message);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

import org.hyperledger.fabric.protos.peer.ChaincodeEventPackage.ChaincodeEvent;
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
import org.hyperledger.fabric.shim.Chaincode.Response;
import org.hyperledger.fabric.shim.ledger.CompositeKey;
import org.hyperledger.fabric.shim.ledger.KeyModification;
import org.hyperledger.fabric.shim.ledger.KeyValue;
Expand Down
Loading

0 comments on commit 4ce3507

Please sign in to comment.