Skip to content

Commit

Permalink
Merge pull request #209 from mbwhite/chaincode-error-completed
Browse files Browse the repository at this point in the history
For 'application errors' set response to the COMPLETED
  • Loading branch information
C0rWin authored Nov 15, 2021
2 parents 32321f4 + b457a96 commit bb959d8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Chaincode.Response executeRequest(final TxFunction txFn, final Invocation
final Throwable cause = e.getCause();

if (cause instanceof ChaincodeException) {
throw (ChaincodeException) cause;
response = ResponseUtils.newErrorResponse(cause);
} else {
throw new ContractRuntimeException("Error during contract method execution", cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ public static Chaincode.Response newErrorResponse(final Throwable throwable) {
if (throwable instanceof ChaincodeException) {
message = throwable.getMessage();
payload = ((ChaincodeException) throwable).getPayload();
return new Chaincode.Response(INTERNAL_SERVER_ERROR, message, payload);
} else {
message = "Unexpected error";
return ResponseUtils.newErrorResponse(message, payload);
}

return ResponseUtils.newErrorResponse(message, payload);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public ChaincodeMessage call() {
// Send ERROR with entire result.Message as payload
logger.severe(() -> String.format("[%-8.8s] Invoke failed with error code %d. Sending %s",
message.getTxid(), result.getStatus().getCode(), ERROR));
finalResponseMessage = ChaincodeMessageFactory.newErrorEventMessage(message.getChannelId(),
message.getTxid(), result.getMessage(), stub.getEvent());
finalResponseMessage = ChaincodeMessageFactory.newCompletedEventMessage(message.getChannelId(),
message.getTxid(), result, stub.getEvent());
if (span != null) {
span.setStatus(StatusCode.ERROR, result.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import static org.hamcrest.Matchers.is;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.COMPLETED;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.ERROR;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.INIT;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.READY;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.REGISTER;
Expand Down Expand Up @@ -529,7 +528,7 @@ public void testErrorInitInvoke() throws Exception {
final ChaincodeBase cb = new ChaincodeBase() {
@Override
public Response init(final ChaincodeStub stub) {
return ResponseUtils.newErrorResponse("Wrong response1");
return ResponseUtils.newErrorResponse("Wrong response1".getBytes());
}

@Override
Expand Down Expand Up @@ -558,8 +557,9 @@ public Response invoke(final ChaincodeStub stub) {
ChaincodeMockPeer.checkScenarioStepEnded(server, 2, 5000, TimeUnit.MILLISECONDS);

assertThat(server.getLastMessageSend().getType(), is(INIT));
assertThat(server.getLastMessageRcvd().getType(), is(ERROR));
assertThat(server.getLastMessageRcvd().getPayload().toStringUtf8(), is("Wrong response1"));
assertThat(server.getLastMessageRcvd().getType(), is(COMPLETED));
String resp1 = (ProposalResponsePackage.Response.parseFrom(server.getLastMessageRcvd().getPayload()).getPayload().toStringUtf8());
assertThat(resp1, is("Wrong response1"));

final ByteString invokePayload = Chaincode.ChaincodeInput.newBuilder().build().toByteString();
final ChaincodeShim.ChaincodeMessage invokeMsg = MessageUtil.newEventMessage(TRANSACTION, "testChannel", "0",
Expand All @@ -569,8 +569,9 @@ public Response invoke(final ChaincodeStub stub) {

ChaincodeMockPeer.checkScenarioStepEnded(server, 3, 5000, TimeUnit.MILLISECONDS);
assertThat(server.getLastMessageSend().getType(), is(TRANSACTION));
assertThat(server.getLastMessageRcvd().getType(), is(ERROR));
assertThat(server.getLastMessageRcvd().getPayload().toStringUtf8(), is("Wrong response2"));
assertThat(server.getLastMessageRcvd().getType(), is(COMPLETED));
String resp2 = ProposalResponsePackage.Response.parseFrom(server.getLastMessageRcvd().getPayload()).getMessage().toString();
assertThat(resp2, is("Wrong response2"));
}

@Test
Expand Down

0 comments on commit bb959d8

Please sign in to comment.