FISCO-BCOS / web3sdk

java sdk for FISCO BCOS(deprecated, please use FISCO-BCOS/java-sdk)
Apache License 2.0
117 stars 86 forks source link

预编译合约Service返回值为String,需要手动反序列化才能做错误处理 #705

Closed CodingCattwo closed 4 years ago

CodingCattwo commented 4 years ago

预编译合约的Service的交易回值和返回值对于错误处理不方便,即PrecompiledCommon.java中handleTransactionReceit(TransactionReceipt, Web3j)方法,当预编译执行错误时,返回的不是一个(code,msg)的response实体类对象,而是一个String对象。二次开发时只能自己复制实现一次handleTransactionReceipt方法,自己抛错。

public static String handleTransactionReceipt(TransactionReceipt receipt, Web3j web3j)
            throws TransactionException, IOException {
        String status = receipt.getStatus();
        if (!"0x0".equals(status)) {
            throw new TransactionException(
                    StatusCode.getStatusMessage(receipt.getStatus(), receipt.getMessage()));
        } else {
            // status不为0x0即交易成功
            // output中包含了预编译合约的执行结果,此处返回为String,并没有包含执行结果的statusCode,如-50000为permission denied, 实际上返回的只有permission denied的字符串。
            if (receipt.getOutput() != null) {
                return PrecompiledCommon.getJsonStr(receipt.getOutput(), web3j);
            } else {
                throw new TransactionException("Transaction is handled failure.");
            }
        }
    }

判断预编译合约是否执行成功,只能如下反序列后操作: image

ywy2090 commented 4 years ago

web3sdk2.6.2版本已经支持返回TransactionReceipt对象的接口