If the result of a transaction shows that the transaction is executed incorrectly, you can locate the cause based on the error code.
- The
errorCode
field inresponse
indicates the cause of a transaction sending or execution error. - When the sent request is transaction-related, you can obtain a more detailed error code about the transaction execution error based on the transaction return value.
The following shows a contract deployment example.
// Synchronous execution.
DeployContractResponse response = sdk.getContractService().deployContract(request);
// Indicates whether the transaction is sent successfully. If the transaction fails to be sent, use result.getErrorCode() to obtain the error code.
assertTrue(response.isSuccess());
// 0 indicates successful transaction execution, and a non-zero value indicates a transaction execution failure. Locate the cause from the ErrorCode object based on the result value.
assertEquals(0, response.getTransactionReceipt().getResult());
// Asynchronous execution.
int result = sdk.getContractService().asyncDeployContract(
request,
new IAsyncCallback() {
@Override
public void onResponse(int errorCode, Response response) {
// If errorCode is 0, a response has been received from the platform. 20072 indicates a message timeout. 30018 indicates that the number of messages exceeds the maximum queue size. (Set a value for NetworkOption.threadPoolQueueSize as required.)
assertEquals(0, errorCode);
// Indicates whether the transaction is sent successfully. If the transaction fails to be sent, use result.getErrorCode() to obtain the error code.
assertTrue(response.isSuccess());
// 0 indicates successful transaction execution, and a non-zero value indicates a transaction execution failure. Locate the cause from the ErrorCode object based on the result value.
// The response to the callback needs to be converted into the response to the request. For example, DeployContractRequest corresponds to DeployContractResponse.
assertEquals(0, deployContractResponse.getTransactionReceipt().getResult());
}
});