Skip to content

Commit 8e32081

Browse files
committed
feat: add error in GetOwnerAndNameFromId()
1 parent bb31105 commit 8e32081

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

object/chain.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,19 @@ func InvokeContract(chainmakerInfo ChainmakerInfo) (ChainmakerTxInfo, error) {
6161
}
6262

6363
var dataMap map[string]string
64-
if err := json.Unmarshal([]byte(chainmakerInfo.Data), &dataMap); err != nil {
65-
return ChainmakerTxInfo{}, fmt.Errorf("parse json data error: %v", err)
64+
err = json.Unmarshal([]byte(chainmakerInfo.Data), &dataMap)
65+
if err != nil {
66+
return ChainmakerTxInfo{}, fmt.Errorf("InvokeContract() error: %v", err)
6667
}
6768

6869
kvPairs := make([]*common.KeyValuePair, 0, len(dataMap))
6970
for key, value := range dataMap {
7071
if key == "key" {
71-
owner, name := GetOwnerAndNameFromId(value, "/")
72+
owner, name, err := GetOwnerAndNameFromId(value, "/")
73+
if err != nil {
74+
return ChainmakerTxInfo{}, err
75+
}
76+
7277
value = GetIdFromOwnerAndName(owner, name, "_")
7378
}
7479
kvPairs = append(kvPairs, &common.KeyValuePair{
@@ -79,18 +84,18 @@ func InvokeContract(chainmakerInfo ChainmakerInfo) (ChainmakerTxInfo, error) {
7984

8085
resp, err := client.InvokeContract(chainmakerInfo.ContractName, chainmakerInfo.ContractMethod, "", kvPairs, -1, true)
8186
if err != nil {
82-
return ChainmakerTxInfo{}, fmt.Errorf("invoke contract error: %v", err)
87+
return ChainmakerTxInfo{}, fmt.Errorf("InvokeContract() error: %v", err)
8388
}
8489

8590
if resp.Code != common.TxStatusCode_SUCCESS {
86-
return ChainmakerTxInfo{}, fmt.Errorf("invoke contract failed, [result:%s]/[msg:%s]", resp.ContractResult.Result, resp.ContractResult.Message)
91+
return ChainmakerTxInfo{}, fmt.Errorf("InvokeContract() error, result = %s, message = %s", resp.ContractResult.Result, resp.ContractResult.Message)
8792
}
8893

8994
txId := resp.TxId
9095

9196
transactionInfo, err := client.GetTxByTxId(txId)
9297
if err != nil {
93-
return ChainmakerTxInfo{}, fmt.Errorf("query contract error: %v", err)
98+
return ChainmakerTxInfo{}, fmt.Errorf("InvokeContract() error: %v", err)
9499
}
95100

96101
blockInfo, err := client.GetBlockByHeight(transactionInfo.GetBlockHeight(), true)
@@ -123,7 +128,11 @@ func QueryContract(chainmakerInfo ChainmakerInfo) (ChainmakerTxInfo, error) {
123128
resultMap := make(map[string]string)
124129
for _, parameter := range parameters {
125130
if parameter.Key == "key" {
126-
owner, name := GetOwnerAndNameFromId(string(parameter.Value), "_")
131+
owner, name, err := GetOwnerAndNameFromId(string(parameter.Value), "_")
132+
if err != nil {
133+
return ChainmakerTxInfo{}, fmt.Errorf("InvokeContract() error: %v", err)
134+
}
135+
127136
resultMap[parameter.Key] = GetIdFromOwnerAndName(owner, name, "/")
128137
} else {
129138
resultMap[parameter.Key] = string(parameter.Value)
@@ -168,5 +177,6 @@ func createClient(config ChainConfig) (*chainmakersdk.ChainClient, error) {
168177
if err != nil {
169178
return nil, fmt.Errorf("failed to create chain client: %v", err)
170179
}
180+
171181
return client, nil
172182
}

object/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package object
22

33
import (
4-
"errors"
54
"fmt"
65
"strings"
76
)
87

9-
func GetOwnerAndNameFromId(id string, separator string) (string, string) {
8+
func GetOwnerAndNameFromId(id string, separator string) (string, string, error) {
109
tokens := strings.Split(id, separator)
1110
if len(tokens) != 2 {
12-
panic(errors.New("GetOwnerAndNameFromId() error, wrong token count for ID: " + id))
11+
return "", "", fmt.Errorf("GetOwnerAndNameFromId() error, wrong token count for ID: %s", id)
1312
}
1413

15-
return tokens[0], tokens[1]
14+
return tokens[0], tokens[1], nil
1615
}
1716

1817
func GetIdFromOwnerAndName(owner string, name string, separator string) string {

0 commit comments

Comments
 (0)