-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-15615] Add ChaincodeException support
These changes allow chaincode to send extended failure information to client applications by including a response payload in a new ChaincodeException. The payload could include a simple error code, or more complex error object depending on requirements. Also includes related error handling changes to prevent potentially sensitive stack traces being sent to client applications. Change-Id: Ib87efdb11abf0330e3ee87ecd988408f94b51c8d Signed-off-by: James Taylor <[email protected]>
- Loading branch information
Showing
12 changed files
with
374 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.fabric.shim; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
/** | ||
* Contracts should use {@code ChaincodeException} to indicate when an error | ||
* occurs in Smart Contract logic. | ||
* | ||
* <p> | ||
* When a {@code ChaincodeException} is thrown an error response will be | ||
* returned from the chaincode container containing the exception message and | ||
* payload, if specified. | ||
* | ||
* <p> | ||
* {@code ChaincodeException} may be extended to provide application specific | ||
* error information. Subclasses should ensure that {@link #getPayload} returns | ||
* a serialized representation of the error in a suitable format for client | ||
* applications to process. | ||
*/ | ||
public class ChaincodeException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 3664437023130016393L; | ||
|
||
private byte[] payload; | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with no detail message. | ||
*/ | ||
public ChaincodeException() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with the specified detail | ||
* message. | ||
* | ||
* @param message the detail message. | ||
*/ | ||
public ChaincodeException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with the specified cause. | ||
* | ||
* @param cause the cause. | ||
*/ | ||
public ChaincodeException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with the specified detail | ||
* message and cause. | ||
* | ||
* @param message the detail message. | ||
* @param cause the cause. | ||
*/ | ||
public ChaincodeException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with the specified detail | ||
* message and response payload. | ||
* | ||
* @param message the detail message. | ||
* @param payload the response payload. | ||
*/ | ||
public ChaincodeException(String message, byte[] payload) { | ||
super(message); | ||
|
||
this.payload = payload; | ||
} | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with the specified detail | ||
* message, response payload and cause. | ||
* | ||
* @param message the detail message. | ||
* @param payload the response payload. | ||
* @param cause the cause. | ||
*/ | ||
public ChaincodeException(String message, byte[] payload, Throwable cause) { | ||
super(message, cause); | ||
|
||
this.payload = payload; | ||
} | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with the specified detail | ||
* message and response payload. | ||
* | ||
* @param message the detail message. | ||
* @param payload the response payload. | ||
*/ | ||
public ChaincodeException(String message, String payload) { | ||
super(message); | ||
|
||
this.payload = payload.getBytes(UTF_8); | ||
} | ||
|
||
/** | ||
* Constructs a new {@code ChaincodeException} with the specified detail | ||
* message, response payload and cause. | ||
* | ||
* @param message the detail message. | ||
* @param payload the response payload. | ||
* @param cause the cause. | ||
*/ | ||
public ChaincodeException(String message, String payload, Throwable cause) { | ||
super(message, cause); | ||
|
||
this.payload = payload.getBytes(UTF_8); | ||
} | ||
|
||
/** | ||
* Returns the response payload or {@code null} if there is no response. | ||
* | ||
* <p> | ||
* The payload should represent the chaincode error in a way that client | ||
* applications written in different programming languages can interpret. For | ||
* example it could include a domain specific error code, in addition to any | ||
* state information which would allow client applications to respond | ||
* appropriately. | ||
* | ||
* @return the response payload or {@code null} if there is no response. | ||
*/ | ||
public byte[] getPayload() { | ||
return payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.